class_list_op 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d357785ffdda8c49dadf2b3220b8359ed4255ab79a95e4397787d8b9f8693909
4
+ data.tar.gz: 3c3c1b9f81125e785106fb069a2a5da4bb282fdea1de8edf2eb1429f8d44ff1e
5
+ SHA512:
6
+ metadata.gz: 60936a499d423937b8f4ab088619500362f2c693f8d91632f6e7bd239db8e0a5aef2f8c9ec31155bce37f0d3a6749eb09eebacd0774af77a7411de4798fea943
7
+ data.tar.gz: '0641594aaa67ad7fab30707d306c9eef87dd78c03d452a018e159c6c5b60c06a2f8ef767fa779f59a09c26001e706c20b6a2d4342fffd9d2735c81b89d99f2cf'
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2026-04-05
4
+
5
+ - Initial release
@@ -0,0 +1,10 @@
1
+ # Code of Conduct
2
+
3
+ "class_list_op" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
4
+
5
+ * Participants will be tolerant of opposing views.
6
+ * Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
7
+ * When interpreting the words and actions of others, participants should always assume good intentions.
8
+ * Behaviour which can be reasonably considered harassment will not be tolerated.
9
+
10
+ If you have any concerns about behaviour within this project, please contact us at ["alexander.s.fokin@gmail.com"](mailto:"alexander.s.fokin@gmail.com").
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Alexander
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,120 @@
1
+ # class_list_op
2
+
3
+ `class_list_op` is a rendering-agnostic Ruby gem for deterministic CSS class token composition.
4
+
5
+ It does not know anything about Rails, ViewComponent, Phlex, Tailwind, or HTML rendering. It only:
6
+
7
+ - normalizes class input
8
+ - applies class operations
9
+ - returns tokens or a resolved string
10
+
11
+ Public namespace:
12
+
13
+ ```ruby
14
+ ClassList
15
+ ```
16
+
17
+ ## Installation
18
+
19
+ Install the gem and add to the application's Gemfile by executing:
20
+
21
+ ```bash
22
+ bundle add class_list_op
23
+ ```
24
+
25
+ If bundler is not being used to manage dependencies, install the gem by executing:
26
+
27
+ ```bash
28
+ gem install class_list_op
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ ```ruby
34
+ require "class_list_op"
35
+
36
+ ClassList.normalize(["flex gap-4", ["mb-2", nil, ""]])
37
+ # => ["flex", "gap-4", "mb-2"]
38
+
39
+ ClassList.resolve("flex gap-4", add: "mb-4")
40
+ # => "flex gap-4 mb-4"
41
+
42
+ ClassList.resolve("flex gap-4", remove: "gap-4")
43
+ # => "flex"
44
+
45
+ ClassList.resolve("flex gap-4", replace: "mb-4")
46
+ # => "mb-4"
47
+
48
+ ClassList.call("flex gap-4", add: :hidden)
49
+ # => "flex gap-4 hidden"
50
+
51
+ list = ClassList.list("flex gap-4")
52
+ updated = list.remove("gap-4").add("mb-4")
53
+
54
+ list.to_s
55
+ # => "flex gap-4"
56
+
57
+ updated.tokens
58
+ # => ["flex", "mb-4"]
59
+ ```
60
+
61
+ ### Supported inputs
62
+
63
+ `ClassList.normalize` and operation values accept:
64
+
65
+ - `String`
66
+ - `Array`
67
+ - `Symbol`
68
+ - `nil`
69
+
70
+ Normalization rules:
71
+
72
+ - `nil` is ignored
73
+ - strings are split by whitespace
74
+ - arrays are flattened recursively
75
+ - empty strings are ignored
76
+ - unsupported types raise `ClassList::InvalidInputError`
77
+
78
+ ### Operations
79
+
80
+ Supported operations:
81
+
82
+ - `add`
83
+ - `remove`
84
+ - `replace`
85
+
86
+ Rules:
87
+
88
+ - `replace` cannot be combined with `add` or `remove`
89
+ - operations are applied in this order: `base -> remove -> add -> uniq`
90
+ - deduplication is stable and keeps the first occurrence
91
+
92
+ Example for component builders:
93
+
94
+ ```ruby
95
+ defaults = "cols w-full md:flex md:flex-row md:space-x-4"
96
+
97
+ ClassList.resolve(defaults, add: "mb-4")
98
+ # => "cols w-full md:flex md:flex-row md:space-x-4 mb-4"
99
+
100
+ ClassList.resolve(defaults, remove: "md:space-x-4", add: "md:space-x-6")
101
+ # => "cols w-full md:flex md:flex-row md:space-x-6"
102
+ ```
103
+
104
+ ## Development
105
+
106
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
107
+
108
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
109
+
110
+ ## Contributing
111
+
112
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alexander-fokin/class_list_op. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/alexander-fokin/class_list_op/blob/main/CODE_OF_CONDUCT.md).
113
+
114
+ ## License
115
+
116
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
117
+
118
+ ## Code of Conduct
119
+
120
+ Everyone interacting in the `class_list_op` project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/alexander-fokin/class_list_op/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ClassList
4
+ Error = Class.new(StandardError)
5
+ InvalidInputError = Class.new(Error)
6
+ InvalidOperationError = Class.new(Error)
7
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ClassList
4
+ class List
5
+ attr_reader :tokens
6
+
7
+ def initialize(base = nil)
8
+ @tokens = ClassList.normalize(base).freeze
9
+ end
10
+
11
+ def add(value)
12
+ self.class.from_tokens(Resolver.call(@tokens, add: value))
13
+ end
14
+
15
+ def remove(value)
16
+ self.class.from_tokens(Resolver.call(@tokens, remove: value))
17
+ end
18
+
19
+ def replace(value)
20
+ self.class.from_tokens(Resolver.call(@tokens, replace: value))
21
+ end
22
+
23
+ def to_s
24
+ @tokens.join(" ")
25
+ end
26
+
27
+ def inspect
28
+ %(#<#{self.class.name} #{to_s.inspect}>)
29
+ end
30
+
31
+ def self.from_tokens(tokens)
32
+ instance = allocate
33
+ instance.instance_variable_set(:@tokens, tokens.freeze)
34
+ instance
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ClassList
4
+ module Resolver
5
+ ALLOWED_KEYS = %i[add remove replace].freeze
6
+
7
+ module_function
8
+
9
+ def call(base = nil, **operations)
10
+ validate_operations!(operations)
11
+
12
+ return uniq_tokens(Tokenizer.call(operations[:replace])) if operations.key?(:replace)
13
+
14
+ tokens = Tokenizer.call(base)
15
+ tokens = apply_remove(tokens, operations[:remove])
16
+ tokens = apply_add(tokens, operations[:add])
17
+
18
+ uniq_tokens(tokens)
19
+ end
20
+
21
+ def validate_operations!(operations)
22
+ unknown_keys = operations.keys - ALLOWED_KEYS
23
+
24
+ unless unknown_keys.empty?
25
+ raise InvalidOperationError, "unknown operations: #{unknown_keys.join(', ')}"
26
+ end
27
+
28
+ return if valid_replace_combination?(operations)
29
+
30
+ raise InvalidOperationError, "replace cannot be combined with add/remove"
31
+ end
32
+
33
+ def valid_replace_combination?(operations)
34
+ return true unless operations.key?(:replace)
35
+
36
+ (operations.keys & %i[add remove]).empty?
37
+ end
38
+
39
+ def apply_remove(tokens, value)
40
+ remove_tokens = Tokenizer.call(value)
41
+ return tokens if remove_tokens.empty?
42
+
43
+ tokens.reject { |token| remove_tokens.include?(token) }
44
+ end
45
+
46
+ def apply_add(tokens, value)
47
+ tokens + Tokenizer.call(value)
48
+ end
49
+
50
+ def uniq_tokens(tokens)
51
+ tokens.each_with_object([]) do |token, result|
52
+ result << token unless result.include?(token)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ClassList
4
+ module Tokenizer
5
+ module_function
6
+
7
+ def call(value)
8
+ case value
9
+ when nil
10
+ []
11
+ when String
12
+ value.split(/\s+/).reject(&:empty?)
13
+ when Symbol
14
+ [value.to_s]
15
+ when Array
16
+ value.flat_map { |item| call(item) }
17
+ else
18
+ raise InvalidInputError, "unsupported class input: #{value.class}"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ClassList
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "class_list/version"
4
+ require_relative "class_list/errors"
5
+ require_relative "class_list/tokenizer"
6
+ require_relative "class_list/resolver"
7
+ require_relative "class_list/list"
8
+
9
+ module ClassList
10
+ class << self
11
+ def normalize(value)
12
+ Tokenizer.call(value)
13
+ end
14
+
15
+ def tokens(base = nil, **operations)
16
+ Resolver.call(base, **operations)
17
+ end
18
+
19
+ def resolve(base = nil, **operations)
20
+ tokens(base, **operations).join(" ")
21
+ end
22
+
23
+ def call(base = nil, **operations)
24
+ resolve(base, **operations)
25
+ end
26
+
27
+ def list(base = nil)
28
+ List.new(base)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,32 @@
1
+ module ClassList
2
+ VERSION: String
3
+
4
+ def self.normalize: (String | Symbol | Array[untyped] | nil value) -> Array[String]
5
+ def self.tokens: (?untyped base, **untyped operations) -> Array[String]
6
+ def self.resolve: (?untyped base, **untyped operations) -> String
7
+ def self.call: (?untyped base, **untyped operations) -> String
8
+ def self.list: (?untyped base) -> List
9
+ end
10
+
11
+ module ClassList::Tokenizer
12
+ def self.call: (String | Symbol | Array[untyped] | nil value) -> Array[String]
13
+ end
14
+
15
+ module ClassList::Resolver
16
+ ALLOWED_KEYS: Array[Symbol]
17
+ def self.call: (?untyped base, **untyped operations) -> Array[String]
18
+ end
19
+
20
+ class ClassList::List
21
+ @tokens: Array[String]
22
+
23
+ attr_reader tokens: Array[String]
24
+
25
+ def initialize: (?untyped base) -> void
26
+ def add: (String | Symbol | Array[untyped] | nil value) -> ClassList::List
27
+ def remove: (String | Symbol | Array[untyped] | nil value) -> ClassList::List
28
+ def replace: (String | Symbol | Array[untyped] | nil value) -> ClassList::List
29
+ def to_s: () -> String
30
+ def inspect: () -> String
31
+ def self.from_tokens: (Array[String] tokens) -> ClassList::List
32
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: class_list_op
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: A rendering-agnostic Ruby gem for normalizing and composing CSS class
13
+ tokens.
14
+ email:
15
+ - alexander.s.fokin@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - CODE_OF_CONDUCT.md
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - lib/class_list/errors.rb
26
+ - lib/class_list/list.rb
27
+ - lib/class_list/resolver.rb
28
+ - lib/class_list/tokenizer.rb
29
+ - lib/class_list/version.rb
30
+ - lib/class_list_op.rb
31
+ - sig/class_list_op.rbs
32
+ homepage: https://github.com/alexander-fokin/class_list_op
33
+ licenses:
34
+ - MIT
35
+ metadata:
36
+ homepage_uri: https://github.com/alexander-fokin/class_list_op
37
+ source_code_uri: https://github.com/alexander-fokin/class_list_op
38
+ changelog_uri: https://github.com/alexander-fokin/class_list_op/blob/main/CHANGELOG.md
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.2.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 4.0.9
54
+ specification_version: 4
55
+ summary: Deterministic CSS class token composition for Ruby.
56
+ test_files: []