@formvk/shared 0.1.0 → 1.0.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/src/merge.ts DELETED
@@ -1,226 +0,0 @@
1
- import { isFn, isPlainObj } from './checkers'
2
- import { isEmpty, isValid } from './isEmpty'
3
-
4
- function defaultIsMergeableObject(value: any) {
5
- return isNonNullObject(value) && !isSpecial(value)
6
- }
7
-
8
- function isNonNullObject(value: any) {
9
- // TODO: value !== null && typeof value === 'object'
10
- return Boolean(value) && typeof value === 'object'
11
- }
12
-
13
- function isSpecial(value: any) {
14
- // TODO: use isComplexObject()
15
- if ('$$typeof' in value && '_owner' in value) {
16
- return true
17
- }
18
- if (value._isAMomentObject) {
19
- return true
20
- }
21
- if (value._isJSONSchemaObject) {
22
- return true
23
- }
24
- if (isFn(value.toJS)) {
25
- return true
26
- }
27
- if (isFn(value.toJSON)) {
28
- return true
29
- }
30
- return !isPlainObj(value)
31
- }
32
-
33
- function emptyTarget(val: any) {
34
- return Array.isArray(val) ? [] : {}
35
- }
36
- // @ts-ignore
37
- function cloneUnlessOtherwiseSpecified(value: any, options: Options) {
38
- if (options.clone !== false && options.isMergeableObject?.(value)) {
39
- return deepmerge(emptyTarget(value), value, options)
40
- }
41
- return value
42
- }
43
-
44
- function defaultArrayMerge(target: any, source: any, options: Options) {
45
- return target.concat(source).map(function (element: any) {
46
- return cloneUnlessOtherwiseSpecified(element, options)
47
- })
48
- }
49
-
50
- function getMergeFunction(key: string, options: Options) {
51
- if (!options.customMerge) {
52
- return deepmerge
53
- }
54
- const customMerge = options.customMerge(key)
55
- return typeof customMerge === 'function' ? customMerge : deepmerge
56
- }
57
-
58
- function getEnumerableOwnPropertySymbols(target: any): any {
59
- return Object.getOwnPropertySymbols
60
- ? Object.getOwnPropertySymbols(target).filter(function (symbol) {
61
- return target.propertyIsEnumerable(symbol)
62
- })
63
- : []
64
- }
65
-
66
- function getKeys(target: any) {
67
- if (!isValid(target)) return []
68
- return Object.keys(target).concat(getEnumerableOwnPropertySymbols(target))
69
- }
70
-
71
- function propertyIsOnObject(object: any, property: any) {
72
- /* istanbul ignore next */
73
- try {
74
- return property in object
75
- } catch (_) {
76
- return false
77
- }
78
- }
79
-
80
- // Protects from prototype poisoning and unexpected merging up the prototype chain.
81
- function propertyIsUnsafe(target: any, key: PropertyKey) {
82
- return (
83
- propertyIsOnObject(target, key) && // Properties are safe to merge if they don't exist in the target yet,
84
- !(
85
- Object.hasOwnProperty.call(target, key) && // unsafe if they exist up the prototype chain,
86
- Object.propertyIsEnumerable.call(target, key)
87
- )
88
- ) // and also unsafe if they're nonenumerable.
89
- }
90
-
91
- function mergeObject(target: any, source: any, options: Options) {
92
- const destination = options.assign ? target || {} : {}
93
- if (!options.isMergeableObject?.(target)) return target
94
- if (!options.assign) {
95
- getKeys(target).forEach(function (key) {
96
- destination[key] = cloneUnlessOtherwiseSpecified(target[key], options)
97
- })
98
- }
99
- getKeys(source).forEach(function (key) {
100
- /* istanbul ignore next */
101
- if (propertyIsUnsafe(target, key)) {
102
- return
103
- }
104
- if (isEmpty(target[key])) {
105
- destination[key] = source[key]
106
- } else if (
107
- propertyIsOnObject(target, key) &&
108
- // @ts-ignore
109
- options.isMergeableObject(source[key])
110
- ) {
111
- destination[key] = getMergeFunction(key, options)(target[key], source[key], options)
112
- } else {
113
- destination[key] = cloneUnlessOtherwiseSpecified(source[key], options)
114
- }
115
- })
116
- return destination
117
- }
118
-
119
- interface Options {
120
- arrayMerge?(target: any[], source: any[], options?: Options): any[]
121
- clone?: boolean
122
- assign?: boolean
123
- customMerge?: (key: string, options?: Options) => ((x: any, y: any) => any) | undefined
124
- isMergeableObject?(value: object): boolean
125
- cloneUnlessOtherwiseSpecified?: (value: any, options: Options) => any
126
- }
127
-
128
- // @ts-ignore
129
- function deepmerge(target: any, source: any, options?: Options) {
130
- options = options || {}
131
- options.arrayMerge = options.arrayMerge || defaultArrayMerge
132
- options.isMergeableObject = options.isMergeableObject || defaultIsMergeableObject
133
- // cloneUnlessOtherwiseSpecified is added to `options` so that custom arrayMerge()
134
- // implementations can use it. The caller may not replace it.
135
- options.cloneUnlessOtherwiseSpecified = cloneUnlessOtherwiseSpecified
136
-
137
- const sourceIsArray = Array.isArray(source)
138
- const targetIsArray = Array.isArray(target)
139
- const sourceAndTargetTypesMatch = sourceIsArray === targetIsArray
140
-
141
- if (!sourceAndTargetTypesMatch) {
142
- return cloneUnlessOtherwiseSpecified(source, options)
143
- } else if (sourceIsArray) {
144
- return options.arrayMerge(target, source, options)
145
- } else {
146
- return mergeObject(target, source, options)
147
- }
148
- }
149
-
150
- export function lazyMerge<T extends object | Function>(target: T, ...args: T[]): any {
151
- function _lazyMerge<T extends object | Function>(target: T, source: T): {} {
152
- if (!isValid(source)) return target
153
- if (!isValid(target)) return source
154
- const isTargetObject = typeof target === 'object'
155
- const isSourceObject = typeof source === 'object'
156
- const isTargetFn = typeof target === 'function'
157
- const isSourceFn = typeof source === 'function'
158
- if (!isTargetObject && !isTargetFn) return source
159
- if (!isSourceObject && !isSourceFn) return target
160
- function getTarget() {
161
- return typeof target === 'function' ? (target as Function)() : target
162
- }
163
- function getSource() {
164
- return typeof source === 'function' ? (source as Function)() : source
165
- }
166
- function set(_: object, key: PropertyKey, value: any) {
167
- const source = getSource()
168
- const target = getTarget()
169
- if (key in source) {
170
- // @ts-ignore
171
- source[key] = value
172
- } else if (key in target) {
173
- // @ts-ignore
174
- target[key] = value
175
- } else {
176
- source[key] = value
177
- }
178
- return true
179
- }
180
- function get(_: object, key: PropertyKey) {
181
- const source = getSource()
182
- // @ts-ignore
183
- if (key in source) {
184
- return source[key]
185
- }
186
- // @ts-ignore
187
- return getTarget()[key]
188
- }
189
- function ownKeys() {
190
- const source = getSource()
191
- const target = getTarget()
192
- const keys = Object.keys(target)
193
- for (const key in source) {
194
- if (!(key in target)) {
195
- keys.push(key)
196
- }
197
- }
198
- return keys
199
- }
200
- function getOwnPropertyDescriptor(_: object, key: PropertyKey) {
201
- return {
202
- value: get(_, key),
203
- enumerable: true,
204
- configurable: true,
205
- }
206
- }
207
- function has(_: object, key: PropertyKey) {
208
- if (key in getSource() || key in getTarget()) return true
209
- return false
210
- }
211
- function getPrototypeOf() {
212
- return Object.getPrototypeOf({})
213
- }
214
- return new Proxy(Object.create(null), {
215
- set,
216
- get,
217
- ownKeys,
218
- getPrototypeOf,
219
- getOwnPropertyDescriptor,
220
- has,
221
- }) as any
222
- }
223
- return args.reduce<{}>((buf, arg) => _lazyMerge(buf, arg), target)
224
- }
225
-
226
- export const merge = deepmerge
package/src/middleware.ts DELETED
@@ -1,18 +0,0 @@
1
- export interface IMiddleware<Payload = any, Result = any> {
2
- (payload: Payload, next: (payload?: Payload) => Result): Result
3
- }
4
-
5
- export function applyMiddleware(payload: any, fns: IMiddleware[] = []) {
6
- function compose(payload: any, fns: IMiddleware[]): Promise<any> {
7
- const prevPayload = payload
8
- return Promise.resolve(fns[0](payload, payload => compose(payload ?? prevPayload, fns.slice(1))))
9
- }
10
- return new Promise((resolve, reject) => {
11
- compose(
12
- payload,
13
- fns.concat(payload => {
14
- resolve(payload)
15
- })
16
- ).catch(reject)
17
- })
18
- }
package/src/path.ts DELETED
@@ -1,5 +0,0 @@
1
- import { Path as FormPath } from '@formvk/path'
2
- import type { Pattern as FormPathPattern } from '@formvk/path'
3
-
4
- export { FormPath }
5
- export type { FormPathPattern }
package/src/string.ts DELETED
@@ -1,38 +0,0 @@
1
- // ansiRegex
2
- function ansiRegex() {
3
- const pattern = [
4
- '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\\u0007)',
5
- '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))',
6
- ].join('|')
7
-
8
- return new RegExp(pattern, 'g')
9
- }
10
-
11
- // astralRegex
12
- const regex = '[\uD800-\uDBFF][\uDC00-\uDFFF]'
13
-
14
- function astralRegex(opts?: { exact: boolean }) {
15
- return opts && opts.exact ? new RegExp(`^${regex}$`) : new RegExp(regex, 'g')
16
- }
17
-
18
- // stripAnsi
19
- function stripAnsi(input: any) {
20
- return typeof input === 'string' ? input.replace(ansiRegex(), '') : input
21
- }
22
-
23
- export function stringLength(input: string) {
24
- return stripAnsi(input).replace(astralRegex(), ' ').length
25
- }
26
-
27
- export function lowerCase(input: string) {
28
- return input.toLowerCase()
29
- }
30
-
31
- /** 'validate-error' -> 'ValidateError' */
32
- export function pascalCase(input: string) {
33
- return input
34
- .split(/[-_\s]+/)
35
- .filter(Boolean)
36
- .map(word => word.charAt(0).toUpperCase() + word.slice(1))
37
- .join('')
38
- }
@@ -1,53 +0,0 @@
1
- import { each } from './array'
2
- import type { Subscriber, Subscription } from './checkers'
3
- import { isFn } from './checkers'
4
-
5
- export class Subscribable<Payload = any> {
6
- subscribers: {
7
- index: number
8
- [key: number]: Subscriber<Payload>
9
- } = {
10
- index: 0,
11
- }
12
-
13
- subscription!: Subscription<Payload>
14
-
15
- subscribe = (callback?: Subscriber<Payload>): number | undefined => {
16
- if (isFn(callback)) {
17
- const index: number = this.subscribers.index + 1
18
- this.subscribers[index] = callback
19
- this.subscribers.index++
20
- return index
21
- }
22
- }
23
-
24
- unsubscribe = (index?: number) => {
25
- if (!index && typeof index !== 'number') {
26
- this.subscribers = {
27
- index: 0,
28
- }
29
- } else if (this.subscribers[index]) {
30
- delete this.subscribers[index]
31
- }
32
- }
33
-
34
- notify = (payload: Payload, silent?: boolean) => {
35
- if (this.subscription) {
36
- if (this.subscription && isFn(this.subscription.notify)) {
37
- if (this.subscription.notify.call(this, payload) === false) {
38
- return
39
- }
40
- }
41
- }
42
- if (silent) return
43
- const filter = (payload: Payload) => {
44
- if (this.subscription && isFn(this.subscription.filter)) {
45
- return this.subscription.filter.call(this, payload)
46
- }
47
- return payload
48
- }
49
- each(this.subscribers, (callback: any) => {
50
- if (isFn(callback)) callback(filter(payload))
51
- })
52
- }
53
- }
package/src/uid.ts DELETED
@@ -1,10 +0,0 @@
1
- let IDX = 36,
2
- HEX = ''
3
- while (IDX--) HEX += IDX.toString(36)
4
-
5
- export function uid(len?: number) {
6
- let str = '',
7
- num = len || 11
8
- while (num--) str += HEX[(Math.random() * 36) | 0]
9
- return str
10
- }
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "paths": {},
5
- "types": []
6
- },
7
- "include": ["./src/**/*.ts"],
8
- "exclude": ["./src/**/__tests__/**", "./__tests__/**"]
9
- }
package/tsconfig.json DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "include": ["./src/**/*.ts", "./test/**/*.ts"]
4
- }
package/vite.config.ts DELETED
@@ -1,21 +0,0 @@
1
- import { defineConfig } from 'vite'
2
- import dts from 'vite-plugin-dts'
3
- export default defineConfig({
4
- plugins: [
5
- dts({
6
- tsconfigPath: './tsconfig.build.json',
7
- entryRoot: 'src',
8
- bundleTypes: true,
9
- }),
10
- ],
11
- build: {
12
- lib: {
13
- entry: 'src/index.ts',
14
- name: 'Formvk.Core',
15
- fileName: 'index',
16
- formats: ['es'],
17
- },
18
- sourcemap: true,
19
- target: 'esnext',
20
- },
21
- })