@formvk/path 0.0.0-stage → 0.1.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/dist/index.d.ts +122 -0
- package/dist/index.js +1334 -0
- package/dist/index.js.map +1 -0
- package/package.json +19 -3
- package/src/__tests__/accessor.spec.ts +429 -0
- package/src/__tests__/basic.spec.ts +14 -0
- package/src/__tests__/match.spec.ts +280 -0
- package/src/__tests__/parser.spec.ts +689 -0
- package/src/__tests__/share.spec.ts +43 -0
- package/src/contexts.ts +23 -0
- package/src/destructor.ts +131 -0
- package/src/index.ts +583 -0
- package/src/matcher.ts +224 -0
- package/src/parser.ts +460 -0
- package/src/shared.ts +83 -0
- package/src/tokenizer.ts +306 -0
- package/src/tokens.ts +233 -0
- package/src/types.ts +133 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +4 -0
- package/vite.config.ts +22 -0
- package/README.md +0 -3
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { expect, test } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isAssignable, isEqual } from '../shared'
|
|
4
|
+
|
|
5
|
+
test('isAssignable', () => {
|
|
6
|
+
expect(isAssignable({})).toBeTruthy()
|
|
7
|
+
expect(isAssignable(() => {})).toBeTruthy()
|
|
8
|
+
|
|
9
|
+
expect(isAssignable(1)).toBeFalsy()
|
|
10
|
+
expect(isAssignable('str')).toBeFalsy()
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
test('isEqual', () => {
|
|
14
|
+
const sameObj = {}
|
|
15
|
+
const sameArray: number[] = []
|
|
16
|
+
expect(isEqual('string', 'string')).toBeTruthy()
|
|
17
|
+
expect(isEqual(123, 123)).toBeTruthy()
|
|
18
|
+
expect(isEqual(undefined, undefined)).toBeTruthy()
|
|
19
|
+
expect(isEqual(null, null)).toBeTruthy()
|
|
20
|
+
|
|
21
|
+
expect(isEqual(sameObj, sameObj)).toBeTruthy()
|
|
22
|
+
expect(isEqual(sameArray, sameArray)).toBeTruthy()
|
|
23
|
+
|
|
24
|
+
expect(isEqual([1, '123'], [1, '123'])).toBeTruthy()
|
|
25
|
+
expect(isEqual([1, '123', { a: 1, b: 2 }], [1, '123', { a: 1, b: 2 }])).toBeTruthy()
|
|
26
|
+
expect(isEqual([1, '123', { a: 1, b: 2 }], [1, '123', { a: 1, b: 3 }])).toBeFalsy()
|
|
27
|
+
expect(isEqual([1, '123'], [1, '234'])).toBeFalsy()
|
|
28
|
+
expect(isEqual([], [1])).toBeFalsy()
|
|
29
|
+
expect(isEqual([], {})).toBeFalsy()
|
|
30
|
+
|
|
31
|
+
expect(isEqual({ a: [1, 2, 3] }, { a: [1, 2, 3] })).toBeTruthy()
|
|
32
|
+
expect(isEqual({ a: [1, 2, 3] }, { a: [1, 2, 4] })).toBeFalsy()
|
|
33
|
+
expect(isEqual({ a: 1 }, { a: 11 })).toBeFalsy()
|
|
34
|
+
expect(isEqual({ a: 1 }, { a: 1, b: 2 })).toBeFalsy()
|
|
35
|
+
|
|
36
|
+
const b = { age: '234' }
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
Object.prototype.name = '123'
|
|
40
|
+
expect(isEqual({ name: '123' }, b)).toBeFalsy()
|
|
41
|
+
|
|
42
|
+
expect(isEqual(NaN, NaN)).toBeTruthy()
|
|
43
|
+
})
|
package/src/contexts.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type Context = {
|
|
2
|
+
flag: string
|
|
3
|
+
[key: string]: any
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
function ContextType(flag: string, props?: any): Context {
|
|
7
|
+
return {
|
|
8
|
+
flag,
|
|
9
|
+
...props,
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const bracketContext = ContextType('[]')
|
|
14
|
+
|
|
15
|
+
export const bracketArrayContext = ContextType('[\\d]')
|
|
16
|
+
|
|
17
|
+
export const bracketDContext = ContextType('[[]]')
|
|
18
|
+
|
|
19
|
+
export const parenContext = ContextType('()')
|
|
20
|
+
|
|
21
|
+
export const braceContext = ContextType('{}')
|
|
22
|
+
|
|
23
|
+
export const destructorContext = ContextType('{x}')
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { isNum } from './shared'
|
|
2
|
+
import type { DestructorRules, Node, Segments } from './types'
|
|
3
|
+
import { isArrayPattern, isDestructorExpression, isIdentifier, isObjectPattern } from './types'
|
|
4
|
+
|
|
5
|
+
type Mutators = {
|
|
6
|
+
getIn: (segments: Segments, source: any) => any
|
|
7
|
+
setIn: (segments: Segments, source: any, value: any) => void
|
|
8
|
+
deleteIn?: (segments: Segments, source: any) => any
|
|
9
|
+
existIn?: (segments: Segments, source: any, start: number) => boolean
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const DestructorCache = new Map()
|
|
13
|
+
|
|
14
|
+
function isValid(val: any) {
|
|
15
|
+
return val !== undefined && val !== null
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function getDestructor(source: string) {
|
|
19
|
+
return DestructorCache.get(source)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function setDestructor(source: string, rules: DestructorRules) {
|
|
23
|
+
DestructorCache.set(source, rules)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function parseDestructorRules(node?: Node): DestructorRules {
|
|
27
|
+
const rules: any[] = []
|
|
28
|
+
if (isObjectPattern(node)) {
|
|
29
|
+
let index = 0
|
|
30
|
+
node.properties.forEach(child => {
|
|
31
|
+
rules[index] = {
|
|
32
|
+
path: [],
|
|
33
|
+
}
|
|
34
|
+
rules[index].key = child.key.value
|
|
35
|
+
rules[index].path.push(child.key.value)
|
|
36
|
+
if (isIdentifier(child.value)) {
|
|
37
|
+
rules[index].key = child.value.value
|
|
38
|
+
}
|
|
39
|
+
const basePath = rules[index].path
|
|
40
|
+
const childRules = parseDestructorRules(child.value as Node)
|
|
41
|
+
let k = index
|
|
42
|
+
childRules.forEach(rule => {
|
|
43
|
+
if (rules[k]) {
|
|
44
|
+
rules[k].key = rule.key
|
|
45
|
+
rules[k].path = basePath.concat(rule.path)
|
|
46
|
+
} else {
|
|
47
|
+
rules[k] = {
|
|
48
|
+
key: rule.key,
|
|
49
|
+
path: basePath.concat(rule.path),
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
k++
|
|
53
|
+
})
|
|
54
|
+
if (k > index) {
|
|
55
|
+
index = k
|
|
56
|
+
} else {
|
|
57
|
+
index++
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
return rules
|
|
61
|
+
} else if (isArrayPattern(node)) {
|
|
62
|
+
let index = 0
|
|
63
|
+
node.elements.forEach((child, key) => {
|
|
64
|
+
rules[index] = {
|
|
65
|
+
path: [],
|
|
66
|
+
}
|
|
67
|
+
rules[index].key = key
|
|
68
|
+
rules[index].path.push(key)
|
|
69
|
+
if (isIdentifier(child)) {
|
|
70
|
+
rules[index].key = child.value
|
|
71
|
+
}
|
|
72
|
+
const basePath = rules[index].path
|
|
73
|
+
const childRules = parseDestructorRules(child as Node)
|
|
74
|
+
let k = index
|
|
75
|
+
childRules.forEach(rule => {
|
|
76
|
+
if (rules[k]) {
|
|
77
|
+
rules[k].key = rule.key
|
|
78
|
+
rules[k].path = basePath.concat(rule.path)
|
|
79
|
+
} else {
|
|
80
|
+
rules[k] = {
|
|
81
|
+
key: rule.key,
|
|
82
|
+
path: basePath.concat(rule.path),
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
k++
|
|
86
|
+
})
|
|
87
|
+
if (k > index) {
|
|
88
|
+
index = k
|
|
89
|
+
} else {
|
|
90
|
+
index++
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
return rules
|
|
94
|
+
}
|
|
95
|
+
if (isDestructorExpression(node)) {
|
|
96
|
+
return parseDestructorRules(node.value)
|
|
97
|
+
}
|
|
98
|
+
return rules
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function setInByDestructor(source: any, rules: DestructorRules, value: any, mutators: Mutators) {
|
|
102
|
+
rules.forEach(({ key, path }) => {
|
|
103
|
+
mutators.setIn([key], source, mutators.getIn(path, value))
|
|
104
|
+
})
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function getInByDestructor(source: any, rules: DestructorRules, mutators: Mutators) {
|
|
108
|
+
let response = {}
|
|
109
|
+
if (rules.length) {
|
|
110
|
+
if (isNum(rules[0].path[0])) {
|
|
111
|
+
response = []
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
source = isValid(source) ? source : {}
|
|
115
|
+
rules.forEach(({ key, path }) => {
|
|
116
|
+
mutators.setIn(path, response, source[key])
|
|
117
|
+
})
|
|
118
|
+
return response
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function deleteInByDestructor(source: any, rules: DestructorRules, mutators: Mutators) {
|
|
122
|
+
rules.forEach(({ key }) => {
|
|
123
|
+
mutators.deleteIn?.([key], source)
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function existInByDestructor(source: any, rules: DestructorRules, start: number, mutators: Mutators) {
|
|
128
|
+
return rules.every(({ key }) => {
|
|
129
|
+
return mutators.existIn?.([key], source, start)
|
|
130
|
+
})
|
|
131
|
+
}
|