@nascentdigital/funnel-core 8.0.2 → 8.0.4

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.
@@ -38,7 +38,7 @@ export declare namespace Block {
38
38
  };
39
39
  function normalize<TFields extends Fields>(property: Property<TFields>): Split<TFields>;
40
40
  }
41
- type Config = Record<string, Optional.Maybe<Field.Type.Simple.Configuration>>;
41
+ type Config = Record<string, Field.Type.Simple.Configuration>;
42
42
  namespace Config {
43
43
  type Variant = 'base' | 'mobile' | 'tablet';
44
44
  namespace Variant {
@@ -86,7 +86,7 @@ export declare namespace Block {
86
86
  type ForConfig<T extends Config = Config> = {
87
87
  name?: string;
88
88
  } & {
89
- [key in keyof T]: Field.Data<Optional.InnerType<T[key]>>;
89
+ [key in keyof T]: Field.Data<T[key]>;
90
90
  };
91
91
  function filter(value: any): value is Data;
92
92
  }
@@ -1,15 +1,26 @@
1
- import type { BooleanField, EnumField, TextField } from './base';
1
+ import { EnumField } from './base';
2
2
  import { BooleanConfig, EnumConfig, TextConfig } from './config/index';
3
3
  import type { Field } from './Field';
4
4
  import { FieldTag } from './FieldTag';
5
5
  export declare namespace Config {
6
6
  type Type = BooleanConfig | EnumConfig | TextConfig;
7
+ type Schema = Record<string, Type>;
8
+ type Data<TConfig extends Schema = Schema> = {
9
+ [key in keyof TConfig]: Field.Data<TConfig[key]>;
10
+ };
11
+ type Values<TConfig extends Schema = Schema> = {
12
+ [key in keyof TConfig]: Field.Value<TConfig[key]>;
13
+ };
7
14
  function isConfig(field: Field | Type): field is Config.Type;
8
15
  namespace Tag {
9
16
  const config: FieldTag;
10
17
  const layout: FieldTag;
11
18
  }
12
- const text: (args: TextConfig.ConstructorArgs) => TextField;
13
- const enumeration: <T extends EnumField.Options>(args: EnumConfig.ConstructorArgs<T>) => EnumField<T>;
14
- const boolean: (args: BooleanConfig.ConstructorArgs) => BooleanField;
19
+ function filterSchemas(schemas: Record<string, Type>, ...tags: ReadonlyArray<FieldTag>): Record<string, Type>;
20
+ function getValues<TConfig extends Schema = Schema>(schema: Partial<TConfig>, data: Partial<Data<TConfig>>): Values<TConfig>;
21
+ function getData<TConfig extends Schema = Schema>(schema: Partial<TConfig>, values: Partial<Values<TConfig>>): Data<TConfig>;
22
+ function getDefaults<TConfig extends Schema = Schema>(schema: TConfig): Values<TConfig>;
23
+ const text: (args: TextConfig.ConstructorArgs) => TextConfig;
24
+ const enumeration: <T extends EnumField.Options>(args: EnumConfig.ConstructorArgs<T>) => EnumConfig<T>;
25
+ const boolean: (args: BooleanConfig.ConstructorArgs) => BooleanConfig;
15
26
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Config = void 0;
4
+ const base_1 = require("./base");
4
5
  const index_1 = require("./config/index");
5
6
  // namespace
6
7
  var Config;
@@ -18,6 +19,77 @@ var Config;
18
19
  Tag.config = 'type.config';
19
20
  Tag.layout = 'group.layout';
20
21
  })(Tag = Config.Tag || (Config.Tag = {}));
22
+ // helpers
23
+ function filterSchemas(schemas, ...tags) {
24
+ return Object.entries(schemas).reduce((schemas, [key, schema]) => {
25
+ // add if it matches the tag
26
+ if (tags.every(tag => schema.hasTag(tag))) {
27
+ schemas[key] = schema;
28
+ }
29
+ // keep reducing
30
+ return schemas;
31
+ }, {});
32
+ }
33
+ Config.filterSchemas = filterSchemas;
34
+ function getValues(schema, data) {
35
+ return Object.entries(schema).reduce((values, [key, config]) => {
36
+ var _a, _b, _c, _d, _e, _f;
37
+ // set data
38
+ switch (config.type) {
39
+ case 'boolean':
40
+ values[key] = (_b = (_a = data[key]) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : config.defaultValue;
41
+ break;
42
+ case 'enum':
43
+ values[key] = (_d = (_c = data[key]) === null || _c === void 0 ? void 0 : _c.value) !== null && _d !== void 0 ? _d : config.defaultOption;
44
+ break;
45
+ case 'text':
46
+ values[key] = (_f = (_e = data[key]) === null || _e === void 0 ? void 0 : _e.value) !== null && _f !== void 0 ? _f : '';
47
+ break;
48
+ }
49
+ // keep reducing
50
+ return values;
51
+ }, {});
52
+ }
53
+ Config.getValues = getValues;
54
+ function getData(schema, values) {
55
+ return Object.entries(schema).reduce((data, [key, config]) => {
56
+ var _a, _b, _c;
57
+ // set data
58
+ switch (config.type) {
59
+ case 'boolean':
60
+ data[key] = base_1.BooleanField.Value.toData((_a = values[key]) !== null && _a !== void 0 ? _a : config.defaultValue);
61
+ break;
62
+ case 'enum':
63
+ data[key] = base_1.EnumField.Value.toData((_b = values[key]) !== null && _b !== void 0 ? _b : config.defaultOption);
64
+ break;
65
+ case 'text':
66
+ data[key] = base_1.TextField.Value.toData((_c = values[key]) !== null && _c !== void 0 ? _c : '');
67
+ break;
68
+ }
69
+ // keep reducing
70
+ return data;
71
+ }, {});
72
+ }
73
+ Config.getData = getData;
74
+ function getDefaults(schema) {
75
+ return Object.entries(schema).reduce((values, [key, schema]) => {
76
+ // set default
77
+ switch (schema.type) {
78
+ case 'boolean':
79
+ values[key] = schema.defaultValue;
80
+ break;
81
+ case 'enum':
82
+ values[key] = schema.defaultOption;
83
+ break;
84
+ case 'text':
85
+ values[key] = '';
86
+ break;
87
+ }
88
+ // keep reducing
89
+ return values;
90
+ }, {});
91
+ }
92
+ Config.getDefaults = getDefaults;
21
93
  // factories
22
94
  Config.text = (args) => new index_1.TextConfig(args);
23
95
  Config.enumeration = (args) => new index_1.EnumConfig(args);
@@ -40,8 +40,8 @@ export declare namespace Field {
40
40
  type Ref = AssetField | PageField;
41
41
  type MaybeRef = CollectionField;
42
42
  type NonAggregate = Text | BooleanField | DateField | TimestampField | DynamicPagesField;
43
- type NonRef = NonAggregate | LinkField | FormField | EmbeddedVideoField;
44
- type Configuration = TextField | EnumField | BooleanField;
43
+ type NonRef = NonAggregate | LinkField | FormField | EmbeddedVideoField | Configuration;
44
+ type Configuration = TextConfig | EnumConfig | BooleanConfig;
45
45
  type Aggregate = Complex | Ref;
46
46
  namespace Aggregate {
47
47
  function filter(field: Type): field is Aggregate;
@@ -39,7 +39,9 @@ class Field {
39
39
  return this.modify({ name });
40
40
  }
41
41
  require() {
42
- return this.modify({ required: true });
42
+ return this.required
43
+ ? this
44
+ : this.modify({ required: true });
43
45
  }
44
46
  hasTag(tag) {
45
47
  return this.tags.has(tag);
@@ -16,6 +16,6 @@ export declare namespace BooleanField {
16
16
  semantics?: Semantics;
17
17
  defaultValue?: boolean;
18
18
  };
19
- type Semantics = 'check' | 'toggle';
19
+ type Semantics = 'check' | 'toggle' | 'segment';
20
20
  type ModifyArgs = Omit<Partial<ConstructorArgs>, 'required'>;
21
21
  }
@@ -1,8 +1,12 @@
1
1
  import { BooleanField } from '../base/BooleanField';
2
+ import { FieldTag } from '../FieldTag';
2
3
  export declare class BooleanConfig extends BooleanField {
3
4
  readonly responsive: boolean;
4
5
  constructor({ tags: tagsRaw, ...args }: BooleanConfig.ConstructorArgs);
6
+ require(): BooleanConfig;
5
7
  modify(args: BooleanConfig.ModifyArgs): BooleanConfig;
8
+ makeResponsive(responsive?: boolean): BooleanConfig;
9
+ addTags(...tags: FieldTag[]): BooleanConfig;
6
10
  }
7
11
  export declare namespace BooleanConfig {
8
12
  type ConstructorArgs = BooleanField.ConstructorArgs & {
@@ -26,8 +26,17 @@ class BooleanConfig extends BooleanField_1.BooleanField {
26
26
  // initialize instance variable
27
27
  this.responsive = (_b = args.responsive) !== null && _b !== void 0 ? _b : false;
28
28
  }
29
+ require() {
30
+ return super.require();
31
+ }
29
32
  modify(args) {
30
33
  return new BooleanConfig(Object.assign({ semantics: this.semantics, name: this.name, hint: this.hint, description: this.description, required: this.required, tags: this.tags, defaultValue: this.defaultValue, responsive: this.responsive }, args));
31
34
  }
35
+ makeResponsive(responsive = true) {
36
+ return this.modify({ responsive });
37
+ }
38
+ addTags(...tags) {
39
+ return super.addTags(...tags);
40
+ }
32
41
  }
33
42
  exports.BooleanConfig = BooleanConfig;
@@ -1,15 +1,20 @@
1
1
  import { StringKeyOf } from 'type-fest';
2
2
  import { EnumField } from '../base/EnumField';
3
+ import { Field } from '../Field';
4
+ import { FieldTag } from '../FieldTag';
3
5
  export declare class EnumConfig<TOptions extends EnumField.Options = EnumField.Options> extends EnumField<TOptions> {
4
6
  readonly responsive: boolean;
5
7
  constructor({ tags: tagsRaw, ...args }: EnumConfig.ConstructorArgs<TOptions>);
8
+ require(): EnumConfig<TOptions>;
6
9
  modify({ options, ...args }: EnumConfig.ModifyArgs<TOptions>): EnumConfig<TOptions>;
10
+ rename(name: string): EnumConfig<TOptions>;
7
11
  updateOptions(options: Partial<TOptions>): EnumConfig<TOptions>;
8
12
  removeOption<TKey extends StringKeyOf<TOptions>>(key: TKey): EnumConfig<TOptions>;
9
13
  removeOptions<TKey extends StringKeyOf<TOptions>>(...keys: ReadonlyArray<TKey>): EnumConfig<TOptions>;
10
14
  keepOptions<TKey extends StringKeyOf<TOptions>>(...keys: ReadonlyArray<TKey>): EnumConfig<TOptions>;
11
15
  setDefault<TKey extends StringKeyOf<TOptions>>(key: TKey): EnumConfig<TOptions>;
12
16
  makeResponsive(responsive?: boolean): EnumConfig<TOptions>;
17
+ addTags(...tags: FieldTag[]): EnumConfig<TOptions>;
13
18
  protected new({ responsive, ...args }: EnumConfig.ConstructorArgs<TOptions>): EnumConfig<TOptions>;
14
19
  }
15
20
  export declare namespace EnumConfig {
@@ -19,4 +24,6 @@ export declare namespace EnumConfig {
19
24
  type ModifyArgs<TOptions extends EnumField.Options> = EnumField.ModifyArgs<TOptions> & {
20
25
  responsive?: boolean;
21
26
  };
27
+ function fromData<TField extends EnumConfig>(data: Field.Data<TField> | undefined): EnumField.Value<TField>;
28
+ function toData<TField extends EnumConfig>(value: EnumField.Value<TField>): Field.Data<EnumConfig>;
22
29
  }
@@ -26,11 +26,17 @@ class EnumConfig extends EnumField_1.EnumField {
26
26
  // initialize instance variable
27
27
  this.responsive = (_b = args.responsive) !== null && _b !== void 0 ? _b : false;
28
28
  }
29
+ require() {
30
+ return super.require();
31
+ }
29
32
  modify(_a) {
30
33
  var _b;
31
34
  var { options } = _a, args = __rest(_a, ["options"]);
32
35
  return this.new(Object.assign({ semantics: this.semantics, name: this.name, hint: this.hint, description: this.description, required: this.required, tags: this.tags, options: (_b = options) !== null && _b !== void 0 ? _b : this.options, defaultOption: this.defaultOption, responsive: this.responsive }, args));
33
36
  }
37
+ rename(name) {
38
+ return super.rename(name);
39
+ }
34
40
  updateOptions(options) {
35
41
  return super.updateOptions(options);
36
42
  }
@@ -58,9 +64,27 @@ class EnumConfig extends EnumField_1.EnumField {
58
64
  responsive
59
65
  });
60
66
  }
67
+ addTags(...tags) {
68
+ return super.addTags(...tags);
69
+ }
61
70
  new(_a) {
62
71
  var { responsive } = _a, args = __rest(_a, ["responsive"]);
63
72
  return new EnumConfig(Object.assign(Object.assign({}, args), { responsive: responsive !== null && responsive !== void 0 ? responsive : this.responsive }));
64
73
  }
65
74
  }
66
75
  exports.EnumConfig = EnumConfig;
76
+ // extension
77
+ (function (EnumConfig) {
78
+ // serialization
79
+ function fromData(data) {
80
+ return data === null || data === void 0 ? void 0 : data.value;
81
+ }
82
+ EnumConfig.fromData = fromData;
83
+ function toData(value) {
84
+ return {
85
+ type: 'enum',
86
+ value
87
+ };
88
+ }
89
+ EnumConfig.toData = toData;
90
+ })(EnumConfig || (exports.EnumConfig = EnumConfig = {}));
@@ -1,8 +1,12 @@
1
1
  import { TextField } from '../base/TextField';
2
+ import { FieldTag } from '../FieldTag';
2
3
  export declare class TextConfig extends TextField {
3
4
  readonly responsive: boolean;
4
5
  constructor({ tags: tagsRaw, ...args }: TextConfig.ConstructorArgs);
6
+ require(): TextConfig;
5
7
  modify(args: TextConfig.ModifyArgs): TextConfig;
8
+ makeResponsive(responsive?: boolean): TextConfig;
9
+ addTags(...tags: FieldTag[]): TextConfig;
6
10
  }
7
11
  export declare namespace TextConfig {
8
12
  type ConstructorArgs = TextField.ConstructorArgs & {
@@ -26,8 +26,17 @@ class TextConfig extends TextField_1.TextField {
26
26
  // initialize instance variable
27
27
  this.responsive = (_b = args.responsive) !== null && _b !== void 0 ? _b : false;
28
28
  }
29
+ require() {
30
+ return super.require();
31
+ }
29
32
  modify(args) {
30
33
  return new TextConfig(Object.assign({ name: this.name, hint: this.hint, description: this.description, required: this.required, tags: this.tags, semantics: this.semantics, regex: this.regex, exact: this.exact, minLength: this.minLength, maxLength: this.maxLength, responsive: this.responsive }, args));
31
34
  }
35
+ makeResponsive(responsive = true) {
36
+ return this.modify({ responsive });
37
+ }
38
+ addTags(...tags) {
39
+ return super.addTags(...tags);
40
+ }
32
41
  }
33
42
  exports.TextConfig = TextConfig;
@@ -0,0 +1 @@
1
+ export type If<Predicate extends boolean | undefined, Then, Else = never> = Predicate extends true ? Then : Else;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ export type Mutable<T> = {
2
+ -readonly [P in keyof T]: T[P];
3
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -2,7 +2,9 @@ export * from './BoxedValue';
2
2
  export * from './Callback';
3
3
  export * from './DateString';
4
4
  export * from './FilterConditionally';
5
+ export * from './If';
5
6
  export * from './MaybeConditionalValue';
7
+ export * from './Mutable';
6
8
  export * from './Ordinal';
7
9
  export * from './OverridableStringUnion';
8
10
  export * from './PartialBy';
@@ -18,7 +18,9 @@ __exportStar(require("./BoxedValue"), exports);
18
18
  __exportStar(require("./Callback"), exports);
19
19
  __exportStar(require("./DateString"), exports);
20
20
  __exportStar(require("./FilterConditionally"), exports);
21
+ __exportStar(require("./If"), exports);
21
22
  __exportStar(require("./MaybeConditionalValue"), exports);
23
+ __exportStar(require("./Mutable"), exports);
22
24
  __exportStar(require("./Ordinal"), exports);
23
25
  __exportStar(require("./OverridableStringUnion"), exports);
24
26
  __exportStar(require("./PartialBy"), exports);
@@ -1,3 +1,4 @@
1
+ import { StringKeyOf } from 'type-fest';
1
2
  import { Field } from './Field';
2
3
  import { FieldList } from './FieldList';
3
4
  import { ObjectField } from './ObjectField';
@@ -7,6 +8,11 @@ export declare namespace FieldModel {
7
8
  type Simple = Field | FieldList;
8
9
  type Composite = ObjectField;
9
10
  type Config = Field<Schema.Field.Type.Simple.Configuration>;
11
+ namespace Config {
12
+ type FromSchema<TConfig extends Schema.Config.Schema> = {
13
+ [TKey in StringKeyOf<TConfig>]: Field<TConfig[TKey]>;
14
+ };
15
+ }
10
16
  function filter(value: any): value is FieldModel;
11
17
  type Event = Field.Event | FieldList.Event | ObjectField.Event;
12
18
  namespace Event {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nascentdigital/funnel-core",
3
- "version": "8.0.2",
3
+ "version": "8.0.4",
4
4
  "repository": {
5
5
  "url": "git+https://github.com/nascentdigital/funnel.git",
6
6
  "directory": "libs/core"
@@ -65,5 +65,5 @@
65
65
  "jest-tsd": "^0.2.2",
66
66
  "ts-jest": "^29.3.2"
67
67
  },
68
- "gitHead": "63c8c0aecf4c61227b25965a54c75818d456d8f3"
68
+ "gitHead": "5375c943981bd14ac211a94f5455fdc1d5ba5098"
69
69
  }