@domql/state 2.5.53 → 2.5.64

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/create.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  import { triggerEventOn } from '@domql/event'
4
- import { deepCloneWithExtnd, exec, is, isArray, isFunction, isObject, isUndefined } from '@domql/utils'
4
+ import { deepCloneWithExtend, exec, is, isArray, isFunction, isObject, isUndefined } from '@domql/utils'
5
5
  import { IGNORE_STATE_PARAMS } from './ignore'
6
6
  import {
7
7
  add,
@@ -27,7 +27,7 @@ export const createState = function (element, parent, options) {
27
27
  export const applyInitialState = function (element, parent, options) {
28
28
  const objectizeState = checkForTypes(element)
29
29
  if (objectizeState === false) return parent.state || {}
30
- else element.state = deepCloneWithExtnd(objectizeState, IGNORE_STATE_PARAMS)
30
+ else element.state = deepCloneWithExtend(objectizeState, IGNORE_STATE_PARAMS)
31
31
 
32
32
  const whatInitReturns = triggerEventOn('stateInit', element, options)
33
33
  if (whatInitReturns === false) return element.state
@@ -51,7 +51,7 @@ export const applyInitialState = function (element, parent, options) {
51
51
  const applyDependentState = (element, state) => {
52
52
  const { __ref: ref } = state
53
53
  if (!ref) return
54
- const dependentState = deepCloneWithExtnd(ref, IGNORE_STATE_PARAMS)
54
+ const dependentState = deepCloneWithExtend(ref, IGNORE_STATE_PARAMS)
55
55
  const newDepends = { [element.key]: dependentState }
56
56
  ref.__depends = isObject(ref.__depends)
57
57
  ? { ...ref.__depends, ...newDepends }
@@ -37,7 +37,7 @@ const applyInitialState = function(element, parent, options) {
37
37
  if (objectizeState === false)
38
38
  return parent.state || {};
39
39
  else
40
- element.state = (0, import_utils.deepCloneWithExtnd)(objectizeState, import_ignore.IGNORE_STATE_PARAMS);
40
+ element.state = (0, import_utils.deepCloneWithExtend)(objectizeState, import_ignore.IGNORE_STATE_PARAMS);
41
41
  const whatInitReturns = (0, import_event.triggerEventOn)("stateInit", element, options);
42
42
  if (whatInitReturns === false)
43
43
  return element.state;
@@ -56,7 +56,7 @@ const applyDependentState = (element, state) => {
56
56
  const { __ref: ref } = state;
57
57
  if (!ref)
58
58
  return;
59
- const dependentState = (0, import_utils.deepCloneWithExtnd)(ref, import_ignore.IGNORE_STATE_PARAMS);
59
+ const dependentState = (0, import_utils.deepCloneWithExtend)(ref, import_ignore.IGNORE_STATE_PARAMS);
60
60
  const newDepends = { [element.key]: dependentState };
61
61
  ref.__depends = (0, import_utils.isObject)(ref.__depends) ? { ...ref.__depends, ...newDepends } : newDepends;
62
62
  return dependentState;
@@ -19,16 +19,24 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  var inherit_exports = {};
20
20
  __export(inherit_exports, {
21
21
  checkIfInherits: () => checkIfInherits,
22
- createChangesByKey: () => createChangesByKey,
23
22
  createInheritedState: () => createInheritedState,
23
+ createNestedObjectByKeyPath: () => createNestedObjectByKeyPath,
24
24
  findInheritedState: () => findInheritedState,
25
25
  getChildStateInKey: () => getChildStateInKey,
26
26
  getParentStateInKey: () => getParentStateInKey,
27
+ getRootStateInKey: () => getRootStateInKey,
27
28
  isState: () => isState
28
29
  });
29
30
  module.exports = __toCommonJS(inherit_exports);
30
31
  var import_utils = require("@domql/utils");
31
32
  var import_ignore = require("./ignore");
33
+ const getRootStateInKey = (stateKey, parentState) => {
34
+ if (!stateKey.includes("~/"))
35
+ return;
36
+ const arr = stateKey.split("~/");
37
+ if (arr.length > 1)
38
+ return parentState.__root;
39
+ };
32
40
  const getParentStateInKey = (stateKey, parentState) => {
33
41
  if (!stateKey.includes("../"))
34
42
  return;
@@ -66,11 +74,17 @@ const findInheritedState = (element, parent, options = {}) => {
66
74
  let stateKey = ref.__state;
67
75
  if (!checkIfInherits(element))
68
76
  return;
77
+ const rootState = getRootStateInKey(stateKey, parent.state);
69
78
  let parentState = parent.state;
70
- const findGrandParentState = getParentStateInKey(stateKey, parent.state);
71
- if (findGrandParentState) {
72
- parentState = findGrandParentState;
73
- stateKey = stateKey.replaceAll("../", "");
79
+ if (rootState) {
80
+ parentState = rootState;
81
+ stateKey = stateKey.replaceAll("~/", "");
82
+ } else {
83
+ const findGrandParentState = getParentStateInKey(stateKey, parent.state);
84
+ if (findGrandParentState) {
85
+ parentState = findGrandParentState;
86
+ stateKey = stateKey.replaceAll("../", "");
87
+ }
74
88
  }
75
89
  if (!parentState)
76
90
  return;
@@ -82,7 +96,7 @@ const createInheritedState = (element, parent) => {
82
96
  if ((0, import_utils.isUndefined)(inheritedState))
83
97
  return element.state;
84
98
  if ((0, import_utils.is)(inheritedState)("object", "array")) {
85
- return (0, import_utils.deepCloneWithExtnd)(inheritedState, import_ignore.IGNORE_STATE_PARAMS);
99
+ return (0, import_utils.deepCloneWithExtend)(inheritedState, import_ignore.IGNORE_STATE_PARAMS);
86
100
  } else if ((0, import_utils.is)(inheritedState)("string", "number", "boolean")) {
87
101
  ref.__stateType = typeof inheritedState;
88
102
  return { value: inheritedState };
@@ -101,7 +115,7 @@ const isState = function(state) {
101
115
  return false;
102
116
  return state.update && state.parse && state.clean && state.create && state.parent && state.destroy && state.rootUpdate && state.parentUpdate && state.toggle && state.add && state.apply && state.__element && state.__children;
103
117
  };
104
- const createChangesByKey = (path, value) => {
118
+ const createNestedObjectByKeyPath = (path, value) => {
105
119
  if (!path) {
106
120
  return value || {};
107
121
  }
@@ -112,7 +112,7 @@ const add = function(value, options = {}) {
112
112
  const state = this;
113
113
  if ((0, import_utils.isArray)(state)) {
114
114
  state.push(value);
115
- state.update(state.parse(), { overwrite: "replace", ...options });
115
+ state.update(state.parse(), { overwrite: true, ...options });
116
116
  } else if ((0, import_utils.isObject)(state)) {
117
117
  const key = Object.keys(state).length;
118
118
  state.update({ [key]: value }, options);
@@ -132,12 +132,12 @@ const remove = function(key, options = {}) {
132
132
  };
133
133
  const set = function(val, options = {}) {
134
134
  const state = this;
135
- const value = (0, import_utils.deepCloneWithExtnd)(val);
135
+ const value = (0, import_utils.deepCloneWithExtend)(val);
136
136
  return state.clean({ preventStateUpdate: true, ...options }).update(value, { replace: true, ...options });
137
137
  };
138
138
  const reset = function(options = {}) {
139
139
  const state = this;
140
- const value = (0, import_utils.deepCloneWithExtnd)(state.parse());
140
+ const value = (0, import_utils.deepCloneWithExtend)(state.parse());
141
141
  return state.set(value, { replace: true, ...options });
142
142
  };
143
143
  const apply = function(func, options = {}) {
@@ -90,9 +90,10 @@ const hoistStateUpdate = (state, obj, options) => {
90
90
  const isStringState = stateType === "string" || stateType === "number" || stateType === "boolean";
91
91
  const value = isStringState ? state.value : state.parse();
92
92
  const passedValue = isStringState ? state.value : obj;
93
+ const findRootState = (0, import_inherit.getRootStateInKey)(stateKey, parent.state);
93
94
  const findGrandParentState = (0, import_inherit.getParentStateInKey)(stateKey, parent.state);
94
- const changesValue = (0, import_inherit.createChangesByKey)(stateKey, passedValue);
95
- const targetParent = findGrandParentState || parent.state;
95
+ const changesValue = (0, import_inherit.createNestedObjectByKeyPath)(stateKey, passedValue);
96
+ const targetParent = findRootState || findGrandParentState || parent.state;
96
97
  if (options.replace)
97
98
  (0, import_utils.overwriteDeep)(targetParent, changesValue || value);
98
99
  targetParent.update(changesValue, {
package/inherit.js CHANGED
@@ -1,8 +1,14 @@
1
1
  'use strict'
2
2
 
3
- import { deepCloneWithExtnd, is, isObjectLike, isUndefined } from '@domql/utils'
3
+ import { deepCloneWithExtend, is, isObjectLike, isUndefined } from '@domql/utils'
4
4
  import { IGNORE_STATE_PARAMS } from './ignore'
5
5
 
6
+ export const getRootStateInKey = (stateKey, parentState) => {
7
+ if (!stateKey.includes('~/')) return
8
+ const arr = stateKey.split('~/')
9
+ if (arr.length > 1) return parentState.__root
10
+ }
11
+
6
12
  export const getParentStateInKey = (stateKey, parentState) => {
7
13
  if (!stateKey.includes('../')) return
8
14
  const arr = stateKey.split('../')
@@ -39,11 +45,18 @@ export const findInheritedState = (element, parent, options = {}) => {
39
45
  let stateKey = ref.__state
40
46
  if (!checkIfInherits(element)) return
41
47
 
48
+ const rootState = getRootStateInKey(stateKey, parent.state)
42
49
  let parentState = parent.state
43
- const findGrandParentState = getParentStateInKey(stateKey, parent.state)
44
- if (findGrandParentState) {
45
- parentState = findGrandParentState
46
- stateKey = stateKey.replaceAll('../', '')
50
+
51
+ if (rootState) {
52
+ parentState = rootState
53
+ stateKey = stateKey.replaceAll('~/', '')
54
+ } else {
55
+ const findGrandParentState = getParentStateInKey(stateKey, parent.state)
56
+ if (findGrandParentState) {
57
+ parentState = findGrandParentState
58
+ stateKey = stateKey.replaceAll('../', '')
59
+ }
47
60
  }
48
61
 
49
62
  if (!parentState) return
@@ -56,7 +69,7 @@ export const createInheritedState = (element, parent) => {
56
69
  if (isUndefined(inheritedState)) return element.state
57
70
 
58
71
  if (is(inheritedState)('object', 'array')) {
59
- return deepCloneWithExtnd(inheritedState, IGNORE_STATE_PARAMS)
72
+ return deepCloneWithExtend(inheritedState, IGNORE_STATE_PARAMS)
60
73
  } else if (is(inheritedState)('string', 'number', 'boolean')) {
61
74
  ref.__stateType = typeof inheritedState
62
75
  return { value: inheritedState }
@@ -91,7 +104,7 @@ export const isState = function (state) {
91
104
  // return arrayContainsOtherArray(keys, ['update', 'parse', 'clean', 'create', 'parent', 'rootUpdate'])
92
105
  }
93
106
 
94
- export const createChangesByKey = (path, value) => {
107
+ export const createNestedObjectByKeyPath = (path, value) => {
95
108
  if (!path) {
96
109
  return value || {}
97
110
  }
package/methods.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- import { isArray, deepCloneWithExtnd, isFunction, isObject, isString, removeFromArray, removeFromObject } from '@domql/utils'
3
+ import { isArray, deepCloneWithExtend, isFunction, isObject, isString, removeFromArray, removeFromObject } from '@domql/utils'
4
4
 
5
5
  import { IGNORE_STATE_PARAMS } from './ignore'
6
6
 
@@ -88,7 +88,7 @@ export const add = function (value, options = {}) {
88
88
  const state = this
89
89
  if (isArray(state)) {
90
90
  state.push(value)
91
- state.update(state.parse(), { overwrite: 'replace', ...options })
91
+ state.update(state.parse(), { overwrite: true, ...options })
92
92
  } else if (isObject(state)) {
93
93
  const key = Object.keys(state).length
94
94
  state.update({ [key]: value }, options)
@@ -109,14 +109,14 @@ export const remove = function (key, options = {}) {
109
109
 
110
110
  export const set = function (val, options = {}) {
111
111
  const state = this
112
- const value = deepCloneWithExtnd(val)
112
+ const value = deepCloneWithExtend(val)
113
113
  return state.clean({ preventStateUpdate: true, ...options })
114
114
  .update(value, { replace: true, ...options })
115
115
  }
116
116
 
117
117
  export const reset = function (options = {}) {
118
118
  const state = this
119
- const value = deepCloneWithExtnd(state.parse())
119
+ const value = deepCloneWithExtend(state.parse())
120
120
  return state.set(value, { replace: true, ...options })
121
121
  }
122
122
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domql/state",
3
- "version": "2.5.53",
3
+ "version": "2.5.64",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "module": "dist/esm/index.js",
@@ -31,5 +31,5 @@
31
31
  "@domql/report": "latest",
32
32
  "@domql/utils": "latest"
33
33
  },
34
- "gitHead": "aafcffa2ebe3d9c2fb96bd3adcb05325b62e68ff"
34
+ "gitHead": "ed8570ce73011589d920684013958c081147a39c"
35
35
  }
package/updateState.js CHANGED
@@ -4,7 +4,7 @@ import { report } from '@domql/report'
4
4
  import { triggerEventOnUpdate } from '@domql/event'
5
5
  import { IGNORE_STATE_PARAMS } from './ignore'
6
6
  import { deepMerge, merge, overwriteDeep, overwriteShallow } from '@domql/utils'
7
- import { checkIfInherits, createChangesByKey, findInheritedState, getParentStateInKey } from './inherit'
7
+ import { checkIfInherits, createNestedObjectByKeyPath, findInheritedState, getParentStateInKey, getRootStateInKey } from './inherit'
8
8
 
9
9
  const STATE_UPDATE_OPTIONS = {
10
10
  overwrite: true,
@@ -78,10 +78,11 @@ const hoistStateUpdate = (state, obj, options) => {
78
78
  const value = isStringState ? state.value : state.parse()
79
79
  const passedValue = isStringState ? state.value : obj
80
80
 
81
+ const findRootState = getRootStateInKey(stateKey, parent.state)
81
82
  const findGrandParentState = getParentStateInKey(stateKey, parent.state)
82
- const changesValue = createChangesByKey(stateKey, passedValue)
83
- const targetParent = findGrandParentState || parent.state
84
- if (options.replace) overwriteDeep(targetParent, changesValue || value) // check with createChangesByKey
83
+ const changesValue = createNestedObjectByKeyPath(stateKey, passedValue)
84
+ const targetParent = findRootState || findGrandParentState || parent.state
85
+ if (options.replace) overwriteDeep(targetParent, changesValue || value) // check with createNestedObjectByKeyPath
85
86
  targetParent.update(changesValue, {
86
87
  execStateFunction: false,
87
88
  isHoisted: true,