@json-layout/core 0.10.0 → 0.11.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-layout/core",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "Compilation and state management utilities for JSON Layout.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -59,7 +59,7 @@
59
59
  },
60
60
  "homepage": "https://github.com/json-layout/json-layout#readme",
61
61
  "dependencies": {
62
- "@json-layout/vocabulary": "^0.10.0",
62
+ "@json-layout/vocabulary": "^0.11.0",
63
63
  "@types/markdown-it": "^13.0.1",
64
64
  "ajv": "^8.12.0",
65
65
  "ajv-errors": "^3.0.0",
@@ -6,6 +6,7 @@ import addFormats from 'ajv-formats'
6
6
  import ajvErrors from 'ajv-errors'
7
7
  import ajvLocalizeModule from 'ajv-i18n'
8
8
  import MarkdownIt from 'markdown-it'
9
+ import { produce } from 'immer'
9
10
  import i18n from '../i18n/index.js'
10
11
  import { makeSkeletonTree } from './skeleton-tree.js'
11
12
  import { resolveRefs } from './utils/resolve-refs.js'
@@ -29,6 +30,17 @@ const ajvLocalize = /** @type {typeof ajvLocalizeModule.default} */ (ajvLocalize
29
30
 
30
31
  // const exprEvalParser = new ExprEvalParser()
31
32
 
33
+ // use Immer for efficient updating with immutability and no-op detection
34
+ /** @type {(draft: PartialCompileOptions, newOptions: PartialCompileOptions) => PartialCompileOptions} */
35
+ export const produceCompileOptions = produce((draft, newOptions) => {
36
+ for (const key of ['ajv', 'ajvOptions', 'code', 'markdown', 'markdownItOptions', 'locale', 'messages', 'optionsKeys']) {
37
+ // @ts-ignore
38
+ if (key in newOptions) draft[key] = newOptions[key]
39
+ // @ts-ignore
40
+ else delete draft[key]
41
+ }
42
+ })
43
+
32
44
  /**
33
45
  * @param {PartialCompileOptions} partialOptions
34
46
  * @returns {CompileOptions}
@@ -1,5 +1,5 @@
1
1
  // import Debug from 'debug'
2
- import { normalizeLayoutFragment, isSwitchStruct, isGetItemsExpression, isGetItemsFetch, isItemsLayout } from '@json-layout/vocabulary'
2
+ import { normalizeLayoutFragment, isSwitchStruct, isGetItemsExpression, isGetItemsFetch, isItemsLayout, getSchemaFragmentType } from '@json-layout/vocabulary'
3
3
  import { makeSkeletonTree } from './skeleton-tree.js'
4
4
 
5
5
  /**
@@ -27,8 +27,7 @@ export function makeSkeletonNode (
27
27
  parentPointer,
28
28
  required
29
29
  ) {
30
- // consolidate schema
31
- if (!schema.type && schema.properties) schema.type = 'object'
30
+ const { type, nullable } = getSchemaFragmentType(schema)
32
31
 
33
32
  // improve on ajv error messages based on ajv-errors (https://ajv.js.org/packages/ajv-errors.html)
34
33
  schema.errorMessage = schema.errorMessage ?? {}
@@ -49,8 +48,9 @@ export function makeSkeletonNode (
49
48
  let defaultData
50
49
  if ('default' in schema) defaultData = schema.default
51
50
  else if (required) {
52
- if (schema.type === 'object') defaultData = {}
53
- if (schema.type === 'array') defaultData = []
51
+ if (nullable) defaultData = null
52
+ else if (type === 'object') defaultData = {}
53
+ else if (type === 'array') defaultData = []
54
54
  }
55
55
 
56
56
  let pure = true
@@ -103,7 +103,7 @@ export function makeSkeletonNode (
103
103
 
104
104
  /** @type {import('./types.js').SkeletonNode} */
105
105
  const node = { key: key ?? '', pointer, parentPointer, pure, propertyKeys: [], roPropertyKeys: [] }
106
- if (schema.type === 'object') {
106
+ if (type === 'object') {
107
107
  if (schema.properties) {
108
108
  node.children = node.children ?? []
109
109
  for (const propertyKey of Object.keys(schema.properties)) {
@@ -166,7 +166,7 @@ export function makeSkeletonNode (
166
166
  const childrenTrees = []
167
167
  /** @type {string[]} */
168
168
  for (let i = 0; i < schema.oneOf.length; i++) {
169
- if (!schema.oneOf[i].type) schema.oneOf[i].type = schema.type
169
+ if (!schema.oneOf[i].type) schema.oneOf[i].type = type
170
170
  const title = schema.oneOf[i].title ?? `option ${i}`
171
171
  delete schema.oneOf[i].title
172
172
  childrenTrees.push(makeSkeletonTree(
@@ -195,7 +195,7 @@ export function makeSkeletonNode (
195
195
  }
196
196
  }
197
197
 
198
- if (schema.type === 'array' && schema.items) {
198
+ if (type === 'array' && schema.items) {
199
199
  if (Array.isArray(schema.items)) {
200
200
  node.children = schema.items.map((/** @type {any} */ itemSchema, /** @type {number} */ i) => {
201
201
  return makeSkeletonNode(
@@ -5,6 +5,8 @@
5
5
  */
6
6
  export function compile(_schema: object, partialOptions?: import("./types.js").PartialCompileOptions | undefined): CompiledLayout;
7
7
  export { resolveRefs } from "./utils/resolve-refs.js";
8
+ /** @type {(draft: PartialCompileOptions, newOptions: PartialCompileOptions) => PartialCompileOptions} */
9
+ export const produceCompileOptions: (draft: PartialCompileOptions, newOptions: PartialCompileOptions) => PartialCompileOptions;
8
10
  export type SkeletonTree = import('./types.js').SkeletonTree;
9
11
  export type SkeletonNode = import('./types.js').SkeletonNode;
10
12
  export type CompiledLayout = import('./types.js').CompiledLayout;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/compile/index.js"],"names":[],"mappings":"AAsEA;;;;GAIG;AACH,iCAJW,MAAM,4EAEJ,cAAc,CA6E1B;;2BAtIY,OAAO,YAAY,EAAE,YAAY;2BACjC,OAAO,YAAY,EAAE,YAAY;6BACjC,OAAO,YAAY,EAAE,cAAc;6BACnC,OAAO,YAAY,EAAE,cAAc;oCACnC,OAAO,YAAY,EAAE,qBAAqB;iCAC1C,OAAO,YAAY,EAAE,kBAAkB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/compile/index.js"],"names":[],"mappings":"AAkFA;;;;GAIG;AACH,iCAJW,MAAM,4EAEJ,cAAc,CA6E1B;;AAjID,yGAAyG;AACzG,4CADmB,qBAAqB,cAAc,qBAAqB,KAAK,qBAAqB,CAQnG;2BAxBW,OAAO,YAAY,EAAE,YAAY;2BACjC,OAAO,YAAY,EAAE,YAAY;6BACjC,OAAO,YAAY,EAAE,cAAc;6BACnC,OAAO,YAAY,EAAE,cAAc;oCACnC,OAAO,YAAY,EAAE,qBAAqB;iCAC1C,OAAO,YAAY,EAAE,kBAAkB"}