@domql/state 2.3.151 → 2.4.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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2016 symbo.ls
3
+ Copyright (c) 2023 symbo.ls
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/createState.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  import { triggerEventOn } from '@domql/event'
4
- import { deepClone, exec, is, isFunction, isObject } from '@domql/utils'
4
+ import { deepClone, exec, is, isArray, isFunction, isObject } from '@domql/utils'
5
5
  import { IGNORE_STATE_PARAMS } from './ignore'
6
6
  import { add, apply, clean, destroy, parentUpdate, parse, remove, rootUpdate, set, toggle } from './methods'
7
7
  import { updateState } from './updateState'
@@ -72,6 +72,17 @@ const checkForTypes = (element) => {
72
72
  return false
73
73
  }
74
74
 
75
+ const addProtoToArray = (state, proto) => {
76
+ for (const key in proto) {
77
+ Object.defineProperty(state, key, {
78
+ value: proto[key],
79
+ enumerable: false, // Set this to true if you want the method to appear in for...in loops
80
+ configurable: true, // Set this to true if you want to allow redefining/removing the property later
81
+ writable: true // Set this to true if you want to allow changing the function later
82
+ })
83
+ }
84
+ }
85
+
75
86
  const applyMethods = (element) => {
76
87
  const state = element.state
77
88
  const ref = element.__ref
@@ -95,7 +106,11 @@ const applyMethods = (element) => {
95
106
  __root: ref.__root ? ref.__root.state : state
96
107
  }
97
108
 
98
- Object.setPrototypeOf(state, proto)
109
+ if (isArray(state)) {
110
+ addProtoToArray(state, proto)
111
+ } else {
112
+ Object.setPrototypeOf(state, proto)
113
+ }
99
114
 
100
115
  if (state.parent) state.parent.__children[element.key] = state
101
116
  }
@@ -85,6 +85,19 @@ const checkForTypes = (element) => {
85
85
  }
86
86
  return false;
87
87
  };
88
+ const addProtoToArray = (state, proto) => {
89
+ for (const key in proto) {
90
+ Object.defineProperty(state, key, {
91
+ value: proto[key],
92
+ enumerable: false,
93
+ // Set this to true if you want the method to appear in for...in loops
94
+ configurable: true,
95
+ // Set this to true if you want to allow redefining/removing the property later
96
+ writable: true
97
+ // Set this to true if you want to allow changing the function later
98
+ });
99
+ }
100
+ };
88
101
  const applyMethods = (element) => {
89
102
  const state = element.state;
90
103
  const ref = element.__ref;
@@ -106,7 +119,11 @@ const applyMethods = (element) => {
106
119
  __children: {},
107
120
  __root: ref.__root ? ref.__root.state : state
108
121
  };
109
- Object.setPrototypeOf(state, proto);
122
+ if ((0, import_utils.isArray)(state)) {
123
+ addProtoToArray(state, proto);
124
+ } else {
125
+ Object.setPrototypeOf(state, proto);
126
+ }
110
127
  if (state.parent)
111
128
  state.parent.__children[element.key] = state;
112
129
  };
@@ -47,6 +47,8 @@ const getChildStateInKey = (stateKey, parentState, options = {}) => {
47
47
  for (let i = 0; i < arrLength; i++) {
48
48
  const childKey = arr[i];
49
49
  const grandChildKey = arr[i + 1];
50
+ if (childKey === "__proto__" || grandChildKey === "__proto__")
51
+ return;
50
52
  let childInParent = parentState[childKey];
51
53
  if (!childInParent)
52
54
  childInParent = parentState[childKey] = {};
@@ -97,8 +99,7 @@ const checkIfInherits = (element) => {
97
99
  const isState = function(state) {
98
100
  if (!(0, import_utils.isObjectLike)(state))
99
101
  return false;
100
- const keys = Object.keys(state);
101
- return (0, import_utils.arrayContainsOtherArray)(keys, ["update", "parse", "clean", "create", "parent", "rootUpdate"]);
102
+ 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;
102
103
  };
103
104
  const createChangesByKey = (path, value) => {
104
105
  if (!path) {
package/inherit.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- import { arrayContainsOtherArray, deepClone, is, isNot, isObjectLike } from '@domql/utils'
3
+ import { deepClone, is, isNot, isObjectLike } from '@domql/utils'
4
4
  import { IGNORE_STATE_PARAMS } from './ignore'
5
5
 
6
6
  export const getParentStateInKey = (stateKey, parentState) => {
@@ -21,6 +21,8 @@ export const getChildStateInKey = (stateKey, parentState, options = {}) => {
21
21
  const childKey = arr[i]
22
22
  const grandChildKey = arr[i + 1]
23
23
 
24
+ if (childKey === '__proto__' || grandChildKey === '__proto__') return
25
+
24
26
  let childInParent = parentState[childKey]
25
27
  if (!childInParent) childInParent = parentState[childKey] = {} // check for array
26
28
  if (!childInParent[grandChildKey]) childInParent[grandChildKey] = {} // check for array
@@ -72,8 +74,20 @@ export const checkIfInherits = (element) => {
72
74
 
73
75
  export const isState = function (state) {
74
76
  if (!isObjectLike(state)) return false
75
- const keys = Object.keys(state)
76
- return arrayContainsOtherArray(keys, ['update', 'parse', 'clean', 'create', 'parent', 'rootUpdate'])
77
+ return state.update &&
78
+ state.parse &&
79
+ state.clean &&
80
+ state.create &&
81
+ state.parent &&
82
+ state.destroy &&
83
+ state.rootUpdate &&
84
+ state.parentUpdate &&
85
+ state.toggle &&
86
+ state.add &&
87
+ state.apply &&
88
+ state.__element &&
89
+ state.__children
90
+ // return arrayContainsOtherArray(keys, ['update', 'parse', 'clean', 'create', 'parent', 'rootUpdate'])
77
91
  }
78
92
 
79
93
  export const createChangesByKey = (path, value) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domql/state",
3
- "version": "2.3.151",
3
+ "version": "2.4.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "module": "dist/esm/index.js",
@@ -19,7 +19,7 @@
19
19
  "dist"
20
20
  ],
21
21
  "scripts": {
22
- "copy:package:cjs": "cp ../../.build/package-cjs.json dist/cjs/package.json",
22
+ "copy:package:cjs": "cp ../../build/package-cjs.json dist/cjs/package.json",
23
23
  "build:esm": "npx esbuild *.js --target=es2019 --format=esm --outdir=dist/esm",
24
24
  "build:cjs": "npx esbuild *.js --target=node16 --format=cjs --outdir=dist/cjs",
25
25
  "build:iife": "npx esbuild *.js --target=node16 --format=iife --outdir=dist/iife",
@@ -31,5 +31,5 @@
31
31
  "@domql/report": "latest",
32
32
  "@domql/utils": "latest"
33
33
  },
34
- "gitHead": "2b240bcbceb25fc4ffda75c25232c66a8e183614"
34
+ "gitHead": "d01a7237a0065a5a439a03062064076c5fdc91b7"
35
35
  }