@fraczak/k 6.2.4 → 6.3.2

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.
package/Value.mjs CHANGED
@@ -16,9 +16,9 @@ function fromObject(obj) {
16
16
  const fields = getFields(obj);
17
17
  if (fields.length === 1) {
18
18
  const field = fields[0];
19
- return new Variant(field, fromObject(obj[field]));
19
+ return Value.variant(field, fromObject(obj[field]));
20
20
  }
21
- return new Product(
21
+ return Value.product(
22
22
  fields.reduce( (result, field) => {
23
23
  result[field] = fromObject(obj[field]);
24
24
  return result;
@@ -26,17 +26,26 @@ function fromObject(obj) {
26
26
  );
27
27
  };
28
28
 
29
- class Value {
30
- constructor(type, pattern = null) {
31
- this.type = type;
32
- this.pattern = pattern;
33
- }
34
- toString() {
35
- return `${this.constructor.name}(type: ${this.type})`;
36
- }
37
- toJSON() {
38
- return { type: this.type };
39
- }
29
+ function isObject(value) {
30
+ return value !== null && typeof value === "object";
31
+ }
32
+
33
+ function isProduct(value) {
34
+ return isObject(value) &&
35
+ value.type === "{}" &&
36
+ isObject(value.product) &&
37
+ !Array.isArray(value.product);
38
+ }
39
+
40
+ function isVariant(value) {
41
+ return isObject(value) &&
42
+ value.type === "<>" &&
43
+ typeof value.tag === "string" &&
44
+ Object.hasOwn(value, "value");
45
+ }
46
+
47
+ function isValue(value) {
48
+ return isProduct(value) || isVariant(value);
40
49
  }
41
50
 
42
51
  function toVector(m) {
@@ -147,46 +156,47 @@ function mergePatterns(declared, observed) {
147
156
  function withPattern(value, pattern) {
148
157
  if (!pattern) return value;
149
158
  if (value.pattern === pattern) return value;
150
- if (value instanceof Product) return new Product(value.product, pattern);
151
- if (value instanceof Variant) return new Variant(value.tag, value.value, pattern);
159
+ if (isProduct(value)) return Value.product(value.product, pattern);
160
+ if (isVariant(value)) return Value.variant(value.tag, value.value, pattern);
152
161
  return value;
153
162
  }
154
163
 
155
- class Product extends Value {
156
- constructor(product, pattern = null) {
157
- super("{}", pattern);
158
- this.product = product;
159
- }
160
-
161
- toString() {
162
- return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(',')}}`;
164
+ class Value {
165
+ constructor(fields) {
166
+ Object.assign(this, fields);
163
167
  }
164
168
 
165
- toJSON() {
166
- let vector = toVector(this.product);
167
- if (vector.length > 0) return vector;
168
- return this.product;
169
+ static product(product, pattern = null) {
170
+ return new Value({ type: "{}", pattern, product });
169
171
  }
170
- }
171
172
 
172
- class Variant extends Value {
173
- constructor(tag, value, pattern = null) {
174
- super("<>", pattern);
175
- this.tag = tag;
176
- this.value = value;
173
+ static variant(tag, value, pattern = null) {
174
+ return new Value({ type: "<>", pattern, tag, value });
177
175
  }
178
176
 
179
177
  toString() {
180
- // return `{${JSON.stringify(this.tag)}:${this.value.toString()}}`;
181
- return `${this.value.toString()}|${pLabel(this.tag)}`;
178
+ if (isVariant(this)) {
179
+ // return `{${JSON.stringify(this.tag)}:${this.value.toString()}}`;
180
+ return `${this.value.toString()}|${pLabel(this.tag)}`;
181
+ }
182
+ if (isProduct(this)) {
183
+ return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(',')}}`;
184
+ }
185
+ return String(this.type);
182
186
  }
183
187
 
184
188
  toJSON() {
185
- if (this.value instanceof Product && Object.keys(this.value.product).length === 0) {
186
- return this.tag;
189
+ if (isVariant(this)) {
190
+ if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {
191
+ return this.tag;
192
+ }
193
+ return {[this.tag]: this.value.toJSON()};
187
194
  }
188
- return {[this.tag]: this.value.toJSON()};
195
+ if (!isProduct(this)) return { type: this.type };
196
+ let vector = toVector(this.product);
197
+ if (vector.length > 0) return vector;
198
+ return this.product;
189
199
  }
190
200
  }
191
- export { Value, Product, Variant, fromObject, cloneSubpattern, edgeSubpattern, composePattern, mergePatterns, withPattern };
192
- export default { Value, Product, Variant, fromObject, cloneSubpattern, edgeSubpattern, composePattern, mergePatterns, withPattern };
201
+ export { Value, fromObject, cloneSubpattern, edgeSubpattern, composePattern, mergePatterns, withPattern, isProduct, isVariant, isValue };
202
+ export default { Value, fromObject, cloneSubpattern, edgeSubpattern, composePattern, mergePatterns, withPattern, isProduct, isVariant, isValue };