@plastic-js/plastic 1.0.1

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.
@@ -0,0 +1,42 @@
1
+ // Solid-style splitProps. Partitions a props object into one proxy per key
2
+ // group plus a trailing "rest" proxy. Reads forward through the original
3
+ // `props[key]`, so any getter defined on the source re-runs at the consumer's
4
+ // read site and continues to track signals.
5
+
6
+ const readOnlyTrap = ()=> {
7
+ throw new Error('splitProps result is read-only')
8
+ }
9
+
10
+ const createSplitProxy = (source, predicate)=> new Proxy({}, {
11
+ get: (_, key)=> (predicate(key) ? source[key] : undefined),
12
+ has: (_, key)=> predicate(key) && key in source,
13
+ ownKeys: ()=> Reflect.ownKeys(source).filter(predicate),
14
+ getOwnPropertyDescriptor: (_, key)=> {
15
+ if (!predicate(key) || !(key in source)){
16
+ return undefined
17
+ }
18
+ return {
19
+ enumerable: true,
20
+ configurable: true,
21
+ get: ()=> source[key],
22
+ }
23
+ },
24
+ set: readOnlyTrap,
25
+ deleteProperty: readOnlyTrap,
26
+ })
27
+
28
+ export const splitProps = (props, ...keyGroups)=> {
29
+ const claimed = new Set()
30
+ const groups = keyGroups.map((keys)=> {
31
+ const keySet = new Set(keys)
32
+ keys.forEach((key)=> claimed.add(key))
33
+ return createSplitProxy(props, (key)=> keySet.has(key))
34
+ })
35
+ const rest = createSplitProxy(props, (key)=> !claimed.has(key))
36
+ return [...groups, rest]
37
+ }
38
+
39
+ export const createSplitProps = (...keys)=> {
40
+ const normalizedKeys = Array.isArray(keys[0]) ? keys[0] : keys
41
+ return (props = {})=> splitProps(props, normalizedKeys)
42
+ }
package/src/utils.js ADDED
@@ -0,0 +1,51 @@
1
+ const isObject = (value)=> {
2
+ return value !== null && typeof value === 'object'
3
+ }
4
+
5
+ const isPlainObject = value=> value != null && typeof value === 'object' && !Array.isArray(value)
6
+
7
+ const flattenChildren = children=> children.flat(Infinity)
8
+
9
+ const isEventProp = key=> (/^on[A-Za-z]/).test(key)
10
+
11
+ const normalizeTextNodeValue = (value)=> {
12
+ if (value == null){
13
+ return ''
14
+ }
15
+
16
+ return String(value)
17
+ }
18
+
19
+ const toClassTokens = (value)=> {
20
+ if (typeof value !== 'string'){
21
+ return new Set()
22
+ }
23
+
24
+ return new Set(value
25
+ .split(/\s+/)
26
+ .filter(Boolean))
27
+ }
28
+
29
+ const toClassMap = (value)=> {
30
+ // null
31
+ if(!value){
32
+ return new Map()
33
+ }
34
+ // string
35
+ const tokens = toClassTokens(value)
36
+ const map = new Map()
37
+ tokens.forEach((token)=> {
38
+ map.set(token, true)
39
+ })
40
+ return map
41
+ }
42
+
43
+ export {
44
+ isObject,
45
+ isPlainObject,
46
+ flattenChildren,
47
+ isEventProp,
48
+ normalizeTextNodeValue,
49
+ toClassTokens,
50
+ toClassMap,
51
+ }