@zeus-js/shared 0.1.0-beta.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/README.md +78 -0
- package/dist/shared.cjs.js +167 -0
- package/dist/shared.cjs.prod.js +167 -0
- package/dist/shared.d.ts +90 -0
- package/dist/shared.esm-bundler.js +127 -0
- package/index.js +7 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# @zeus-js/shared
|
|
2
|
+
|
|
3
|
+
Internal utility functions and constants shared across `@zeus-js` packages.
|
|
4
|
+
|
|
5
|
+
## API Reference
|
|
6
|
+
|
|
7
|
+
### General Utilities
|
|
8
|
+
|
|
9
|
+
#### Constants
|
|
10
|
+
|
|
11
|
+
- `EMPTY_OBJ` - Frozen empty object
|
|
12
|
+
- `EMPTY_ARR` - Frozen empty array
|
|
13
|
+
- `NOOP` - No-operation function
|
|
14
|
+
- `NO` - Function that always returns false
|
|
15
|
+
|
|
16
|
+
#### Type Guards
|
|
17
|
+
|
|
18
|
+
- `isArray(val)` - Check if value is an array
|
|
19
|
+
- `isMap(val)` - Check if value is a Map
|
|
20
|
+
- `isSet(val)` - Check if value is a Set
|
|
21
|
+
- `isDate(val)` - Check if value is a Date
|
|
22
|
+
- `isRegExp(val)` - Check if value is a RegExp
|
|
23
|
+
- `isFunction(val)` - Check if value is a function
|
|
24
|
+
- `isString(val)` - Check if value is a string
|
|
25
|
+
- `isSymbol(val)` - Check if value is a symbol
|
|
26
|
+
- `isObject(val)` - Check if value is an object
|
|
27
|
+
- `isPromise(val)` - Check if value is a Promise
|
|
28
|
+
- `isPlainObject(val)` - Check if value is a plain object
|
|
29
|
+
- `isIntegerKey(key)` - Check if key is an integer string
|
|
30
|
+
|
|
31
|
+
#### Event Utilities
|
|
32
|
+
|
|
33
|
+
- `isOn(key)` - Check if key is an event handler (starts with 'on')
|
|
34
|
+
- `isModelListener(key)` - Check if key is a v-model listener
|
|
35
|
+
|
|
36
|
+
#### String Utilities
|
|
37
|
+
|
|
38
|
+
- `camelize(str)` - Convert kebab-case to camelCase
|
|
39
|
+
- `hyphenate(str)` - Convert camelCase to kebab-case
|
|
40
|
+
- `capitalize(str)` - Capitalize first letter
|
|
41
|
+
- `toHandlerKey(str)` - Convert to event handler key (e.g., 'click' -> 'onClick')
|
|
42
|
+
|
|
43
|
+
#### Object Utilities
|
|
44
|
+
|
|
45
|
+
- `extend(...sources)` - Object.assign alias
|
|
46
|
+
- `hasOwn(obj, key)` - Check if object has own property
|
|
47
|
+
- `hasChanged(value, oldValue)` - Check if value has changed (handles NaN)
|
|
48
|
+
- `def(obj, key, value, writable?)` - Define property with defaults
|
|
49
|
+
- `remove(arr, item)` - Remove item from array
|
|
50
|
+
|
|
51
|
+
#### Array Utilities
|
|
52
|
+
|
|
53
|
+
- `invokeArrayFns(fns, ...args)` - Invoke all functions in array
|
|
54
|
+
|
|
55
|
+
#### Type Utilities
|
|
56
|
+
|
|
57
|
+
- `toTypeString(val)` - Get Object.prototype.toString result
|
|
58
|
+
- `toRawType(val)` - Get raw type string
|
|
59
|
+
- `objectToString` - Object.prototype.toString reference
|
|
60
|
+
|
|
61
|
+
#### Number Utilities
|
|
62
|
+
|
|
63
|
+
- `looseToNumber(val)` - Convert to number with loose parsing
|
|
64
|
+
- `toNumber(val)` - Convert string to number
|
|
65
|
+
|
|
66
|
+
#### Global Utilities
|
|
67
|
+
|
|
68
|
+
- `getGlobalThis()` - Get global this (handles different environments)
|
|
69
|
+
|
|
70
|
+
#### Props Utilities
|
|
71
|
+
|
|
72
|
+
- `isReservedProp(key)` - Check if prop is reserved (key, ref, etc.)
|
|
73
|
+
- `isBuiltInDirective(key)` - Check if key is a built-in directive
|
|
74
|
+
|
|
75
|
+
#### Code Generation
|
|
76
|
+
|
|
77
|
+
- `genPropsAccessExp(name)` - Generate props access expression
|
|
78
|
+
- `genCacheKey(source, options)` - Generate cache key for source
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* shared v0.1.0-beta.0
|
|
3
|
+
* (c) 2026 baicie
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
**/
|
|
6
|
+
Object.defineProperties(exports, {
|
|
7
|
+
__esModule: { value: true },
|
|
8
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
9
|
+
});
|
|
10
|
+
//#region packages/core/shared/src/makeMap.ts
|
|
11
|
+
/**
|
|
12
|
+
* Make a map and return a function for checking if a key
|
|
13
|
+
* is in that map.
|
|
14
|
+
* IMPORTANT: all calls of this function must be prefixed with
|
|
15
|
+
* \/\*#\_\_PURE\_\_\*\/
|
|
16
|
+
* So that rollup can tree-shake them if necessary.
|
|
17
|
+
*/
|
|
18
|
+
/*@__NO_SIDE_EFFECTS__*/
|
|
19
|
+
function makeMap(str) {
|
|
20
|
+
const map = Object.create(null);
|
|
21
|
+
for (const key of str.split(",")) map[key] = 1;
|
|
22
|
+
return (val) => val in map;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region packages/core/shared/src/general.ts
|
|
26
|
+
const EMPTY_OBJ = Object.freeze({});
|
|
27
|
+
const EMPTY_ARR = Object.freeze([]);
|
|
28
|
+
const NOOP = () => {};
|
|
29
|
+
/**
|
|
30
|
+
* Always return false.
|
|
31
|
+
*/
|
|
32
|
+
const NO = () => false;
|
|
33
|
+
const isOn = (key) => key.charCodeAt(0) === 111 && key.charCodeAt(1) === 110 && (key.charCodeAt(2) > 122 || key.charCodeAt(2) < 97);
|
|
34
|
+
const isModelListener = (key) => key.startsWith("onUpdate:");
|
|
35
|
+
const extend = Object.assign;
|
|
36
|
+
const remove = (arr, el) => {
|
|
37
|
+
const i = arr.indexOf(el);
|
|
38
|
+
if (i > -1) arr.splice(i, 1);
|
|
39
|
+
};
|
|
40
|
+
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
41
|
+
const hasOwn = (val, key) => hasOwnProperty.call(val, key);
|
|
42
|
+
const isArray = Array.isArray;
|
|
43
|
+
const isMap = (val) => toTypeString(val) === "[object Map]";
|
|
44
|
+
const isSet = (val) => toTypeString(val) === "[object Set]";
|
|
45
|
+
const isDate = (val) => toTypeString(val) === "[object Date]";
|
|
46
|
+
const isRegExp = (val) => toTypeString(val) === "[object RegExp]";
|
|
47
|
+
const isFunction = (val) => typeof val === "function";
|
|
48
|
+
const isString = (val) => typeof val === "string";
|
|
49
|
+
const isSymbol = (val) => typeof val === "symbol";
|
|
50
|
+
const isObject = (val) => val !== null && typeof val === "object";
|
|
51
|
+
const isPromise = (val) => {
|
|
52
|
+
return (isObject(val) || isFunction(val)) && isFunction(val.then) && isFunction(val.catch);
|
|
53
|
+
};
|
|
54
|
+
const objectToString = Object.prototype.toString;
|
|
55
|
+
const toTypeString = (value) => objectToString.call(value);
|
|
56
|
+
const toRawType = (value) => {
|
|
57
|
+
return toTypeString(value).slice(8, -1);
|
|
58
|
+
};
|
|
59
|
+
const isPlainObject = (val) => toTypeString(val) === "[object Object]";
|
|
60
|
+
const isIntegerKey = (key) => isString(key) && key !== "NaN" && key[0] !== "-" && "" + parseInt(key, 10) === key;
|
|
61
|
+
const cacheStringFunction = (fn) => {
|
|
62
|
+
const cache = Object.create(null);
|
|
63
|
+
return ((str) => {
|
|
64
|
+
return cache[str] || (cache[str] = fn(str));
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
const camelizeRE = /-\w/g;
|
|
68
|
+
/**
|
|
69
|
+
* @private
|
|
70
|
+
*/
|
|
71
|
+
const camelize = cacheStringFunction((str) => {
|
|
72
|
+
return str.replace(camelizeRE, (c) => c.slice(1).toUpperCase());
|
|
73
|
+
});
|
|
74
|
+
const hyphenateRE = /\B([A-Z])/g;
|
|
75
|
+
/**
|
|
76
|
+
* @private
|
|
77
|
+
*/
|
|
78
|
+
const hyphenate = cacheStringFunction((str) => str.replace(hyphenateRE, "-$1").toLowerCase());
|
|
79
|
+
/**
|
|
80
|
+
* @private
|
|
81
|
+
*/
|
|
82
|
+
const capitalize = cacheStringFunction((str) => {
|
|
83
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
84
|
+
});
|
|
85
|
+
/**
|
|
86
|
+
* @private
|
|
87
|
+
*/
|
|
88
|
+
const toHandlerKey = cacheStringFunction((str) => {
|
|
89
|
+
return str ? `on${capitalize(str)}` : ``;
|
|
90
|
+
});
|
|
91
|
+
const hasChanged = (value, oldValue) => !Object.is(value, oldValue);
|
|
92
|
+
const invokeArrayFns = (fns, ...arg) => {
|
|
93
|
+
for (let i = 0; i < fns.length; i++) fns[i](...arg);
|
|
94
|
+
};
|
|
95
|
+
const def = (obj, key, value, writable = false) => {
|
|
96
|
+
Object.defineProperty(obj, key, {
|
|
97
|
+
configurable: true,
|
|
98
|
+
enumerable: false,
|
|
99
|
+
writable,
|
|
100
|
+
value
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* "123-foo" will be parsed to 123
|
|
105
|
+
* This is used for the .number modifier in v-model
|
|
106
|
+
*/
|
|
107
|
+
const looseToNumber = (val) => {
|
|
108
|
+
const n = parseFloat(val);
|
|
109
|
+
return isNaN(n) ? val : n;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Only concerns number-like strings
|
|
113
|
+
* "123-foo" will be returned as-is
|
|
114
|
+
*/
|
|
115
|
+
const toNumber = (val) => {
|
|
116
|
+
const n = isString(val) ? Number(val) : NaN;
|
|
117
|
+
return isNaN(n) ? val : n;
|
|
118
|
+
};
|
|
119
|
+
let _globalThis;
|
|
120
|
+
const getGlobalThis = () => {
|
|
121
|
+
return _globalThis || (_globalThis = typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {});
|
|
122
|
+
};
|
|
123
|
+
const identRE = /^[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*$/;
|
|
124
|
+
function genPropsAccessExp(name) {
|
|
125
|
+
return identRE.test(name) ? `__props.${name}` : `__props[${JSON.stringify(name)}]`;
|
|
126
|
+
}
|
|
127
|
+
function genCacheKey(source, options) {
|
|
128
|
+
return source + JSON.stringify(options, (_, val) => typeof val === "function" ? val.toString() : val);
|
|
129
|
+
}
|
|
130
|
+
//#endregion
|
|
131
|
+
exports.EMPTY_ARR = EMPTY_ARR;
|
|
132
|
+
exports.EMPTY_OBJ = EMPTY_OBJ;
|
|
133
|
+
exports.NO = NO;
|
|
134
|
+
exports.NOOP = NOOP;
|
|
135
|
+
exports.camelize = camelize;
|
|
136
|
+
exports.capitalize = capitalize;
|
|
137
|
+
exports.def = def;
|
|
138
|
+
exports.extend = extend;
|
|
139
|
+
exports.genCacheKey = genCacheKey;
|
|
140
|
+
exports.genPropsAccessExp = genPropsAccessExp;
|
|
141
|
+
exports.getGlobalThis = getGlobalThis;
|
|
142
|
+
exports.hasChanged = hasChanged;
|
|
143
|
+
exports.hasOwn = hasOwn;
|
|
144
|
+
exports.hyphenate = hyphenate;
|
|
145
|
+
exports.invokeArrayFns = invokeArrayFns;
|
|
146
|
+
exports.isArray = isArray;
|
|
147
|
+
exports.isDate = isDate;
|
|
148
|
+
exports.isFunction = isFunction;
|
|
149
|
+
exports.isIntegerKey = isIntegerKey;
|
|
150
|
+
exports.isMap = isMap;
|
|
151
|
+
exports.isModelListener = isModelListener;
|
|
152
|
+
exports.isObject = isObject;
|
|
153
|
+
exports.isOn = isOn;
|
|
154
|
+
exports.isPlainObject = isPlainObject;
|
|
155
|
+
exports.isPromise = isPromise;
|
|
156
|
+
exports.isRegExp = isRegExp;
|
|
157
|
+
exports.isSet = isSet;
|
|
158
|
+
exports.isString = isString;
|
|
159
|
+
exports.isSymbol = isSymbol;
|
|
160
|
+
exports.looseToNumber = looseToNumber;
|
|
161
|
+
exports.makeMap = makeMap;
|
|
162
|
+
exports.objectToString = objectToString;
|
|
163
|
+
exports.remove = remove;
|
|
164
|
+
exports.toHandlerKey = toHandlerKey;
|
|
165
|
+
exports.toNumber = toNumber;
|
|
166
|
+
exports.toRawType = toRawType;
|
|
167
|
+
exports.toTypeString = toTypeString;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* shared v0.1.0-beta.0
|
|
3
|
+
* (c) 2026 baicie
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
**/
|
|
6
|
+
Object.defineProperties(exports, {
|
|
7
|
+
__esModule: { value: true },
|
|
8
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
9
|
+
});
|
|
10
|
+
//#region packages/core/shared/src/makeMap.ts
|
|
11
|
+
/**
|
|
12
|
+
* Make a map and return a function for checking if a key
|
|
13
|
+
* is in that map.
|
|
14
|
+
* IMPORTANT: all calls of this function must be prefixed with
|
|
15
|
+
* \/\*#\_\_PURE\_\_\*\/
|
|
16
|
+
* So that rollup can tree-shake them if necessary.
|
|
17
|
+
*/
|
|
18
|
+
/*@__NO_SIDE_EFFECTS__*/
|
|
19
|
+
function makeMap(str) {
|
|
20
|
+
const map = Object.create(null);
|
|
21
|
+
for (const key of str.split(",")) map[key] = 1;
|
|
22
|
+
return (val) => val in map;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region packages/core/shared/src/general.ts
|
|
26
|
+
const EMPTY_OBJ = {};
|
|
27
|
+
const EMPTY_ARR = [];
|
|
28
|
+
const NOOP = () => {};
|
|
29
|
+
/**
|
|
30
|
+
* Always return false.
|
|
31
|
+
*/
|
|
32
|
+
const NO = () => false;
|
|
33
|
+
const isOn = (key) => key.charCodeAt(0) === 111 && key.charCodeAt(1) === 110 && (key.charCodeAt(2) > 122 || key.charCodeAt(2) < 97);
|
|
34
|
+
const isModelListener = (key) => key.startsWith("onUpdate:");
|
|
35
|
+
const extend = Object.assign;
|
|
36
|
+
const remove = (arr, el) => {
|
|
37
|
+
const i = arr.indexOf(el);
|
|
38
|
+
if (i > -1) arr.splice(i, 1);
|
|
39
|
+
};
|
|
40
|
+
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
41
|
+
const hasOwn = (val, key) => hasOwnProperty.call(val, key);
|
|
42
|
+
const isArray = Array.isArray;
|
|
43
|
+
const isMap = (val) => toTypeString(val) === "[object Map]";
|
|
44
|
+
const isSet = (val) => toTypeString(val) === "[object Set]";
|
|
45
|
+
const isDate = (val) => toTypeString(val) === "[object Date]";
|
|
46
|
+
const isRegExp = (val) => toTypeString(val) === "[object RegExp]";
|
|
47
|
+
const isFunction = (val) => typeof val === "function";
|
|
48
|
+
const isString = (val) => typeof val === "string";
|
|
49
|
+
const isSymbol = (val) => typeof val === "symbol";
|
|
50
|
+
const isObject = (val) => val !== null && typeof val === "object";
|
|
51
|
+
const isPromise = (val) => {
|
|
52
|
+
return (isObject(val) || isFunction(val)) && isFunction(val.then) && isFunction(val.catch);
|
|
53
|
+
};
|
|
54
|
+
const objectToString = Object.prototype.toString;
|
|
55
|
+
const toTypeString = (value) => objectToString.call(value);
|
|
56
|
+
const toRawType = (value) => {
|
|
57
|
+
return toTypeString(value).slice(8, -1);
|
|
58
|
+
};
|
|
59
|
+
const isPlainObject = (val) => toTypeString(val) === "[object Object]";
|
|
60
|
+
const isIntegerKey = (key) => isString(key) && key !== "NaN" && key[0] !== "-" && "" + parseInt(key, 10) === key;
|
|
61
|
+
const cacheStringFunction = (fn) => {
|
|
62
|
+
const cache = Object.create(null);
|
|
63
|
+
return ((str) => {
|
|
64
|
+
return cache[str] || (cache[str] = fn(str));
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
const camelizeRE = /-\w/g;
|
|
68
|
+
/**
|
|
69
|
+
* @private
|
|
70
|
+
*/
|
|
71
|
+
const camelize = cacheStringFunction((str) => {
|
|
72
|
+
return str.replace(camelizeRE, (c) => c.slice(1).toUpperCase());
|
|
73
|
+
});
|
|
74
|
+
const hyphenateRE = /\B([A-Z])/g;
|
|
75
|
+
/**
|
|
76
|
+
* @private
|
|
77
|
+
*/
|
|
78
|
+
const hyphenate = cacheStringFunction((str) => str.replace(hyphenateRE, "-$1").toLowerCase());
|
|
79
|
+
/**
|
|
80
|
+
* @private
|
|
81
|
+
*/
|
|
82
|
+
const capitalize = cacheStringFunction((str) => {
|
|
83
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
84
|
+
});
|
|
85
|
+
/**
|
|
86
|
+
* @private
|
|
87
|
+
*/
|
|
88
|
+
const toHandlerKey = cacheStringFunction((str) => {
|
|
89
|
+
return str ? `on${capitalize(str)}` : ``;
|
|
90
|
+
});
|
|
91
|
+
const hasChanged = (value, oldValue) => !Object.is(value, oldValue);
|
|
92
|
+
const invokeArrayFns = (fns, ...arg) => {
|
|
93
|
+
for (let i = 0; i < fns.length; i++) fns[i](...arg);
|
|
94
|
+
};
|
|
95
|
+
const def = (obj, key, value, writable = false) => {
|
|
96
|
+
Object.defineProperty(obj, key, {
|
|
97
|
+
configurable: true,
|
|
98
|
+
enumerable: false,
|
|
99
|
+
writable,
|
|
100
|
+
value
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* "123-foo" will be parsed to 123
|
|
105
|
+
* This is used for the .number modifier in v-model
|
|
106
|
+
*/
|
|
107
|
+
const looseToNumber = (val) => {
|
|
108
|
+
const n = parseFloat(val);
|
|
109
|
+
return isNaN(n) ? val : n;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Only concerns number-like strings
|
|
113
|
+
* "123-foo" will be returned as-is
|
|
114
|
+
*/
|
|
115
|
+
const toNumber = (val) => {
|
|
116
|
+
const n = isString(val) ? Number(val) : NaN;
|
|
117
|
+
return isNaN(n) ? val : n;
|
|
118
|
+
};
|
|
119
|
+
let _globalThis;
|
|
120
|
+
const getGlobalThis = () => {
|
|
121
|
+
return _globalThis || (_globalThis = typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {});
|
|
122
|
+
};
|
|
123
|
+
const identRE = /^[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*$/;
|
|
124
|
+
function genPropsAccessExp(name) {
|
|
125
|
+
return identRE.test(name) ? `__props.${name}` : `__props[${JSON.stringify(name)}]`;
|
|
126
|
+
}
|
|
127
|
+
function genCacheKey(source, options) {
|
|
128
|
+
return source + JSON.stringify(options, (_, val) => typeof val === "function" ? val.toString() : val);
|
|
129
|
+
}
|
|
130
|
+
//#endregion
|
|
131
|
+
exports.EMPTY_ARR = EMPTY_ARR;
|
|
132
|
+
exports.EMPTY_OBJ = EMPTY_OBJ;
|
|
133
|
+
exports.NO = NO;
|
|
134
|
+
exports.NOOP = NOOP;
|
|
135
|
+
exports.camelize = camelize;
|
|
136
|
+
exports.capitalize = capitalize;
|
|
137
|
+
exports.def = def;
|
|
138
|
+
exports.extend = extend;
|
|
139
|
+
exports.genCacheKey = genCacheKey;
|
|
140
|
+
exports.genPropsAccessExp = genPropsAccessExp;
|
|
141
|
+
exports.getGlobalThis = getGlobalThis;
|
|
142
|
+
exports.hasChanged = hasChanged;
|
|
143
|
+
exports.hasOwn = hasOwn;
|
|
144
|
+
exports.hyphenate = hyphenate;
|
|
145
|
+
exports.invokeArrayFns = invokeArrayFns;
|
|
146
|
+
exports.isArray = isArray;
|
|
147
|
+
exports.isDate = isDate;
|
|
148
|
+
exports.isFunction = isFunction;
|
|
149
|
+
exports.isIntegerKey = isIntegerKey;
|
|
150
|
+
exports.isMap = isMap;
|
|
151
|
+
exports.isModelListener = isModelListener;
|
|
152
|
+
exports.isObject = isObject;
|
|
153
|
+
exports.isOn = isOn;
|
|
154
|
+
exports.isPlainObject = isPlainObject;
|
|
155
|
+
exports.isPromise = isPromise;
|
|
156
|
+
exports.isRegExp = isRegExp;
|
|
157
|
+
exports.isSet = isSet;
|
|
158
|
+
exports.isString = isString;
|
|
159
|
+
exports.isSymbol = isSymbol;
|
|
160
|
+
exports.looseToNumber = looseToNumber;
|
|
161
|
+
exports.makeMap = makeMap;
|
|
162
|
+
exports.objectToString = objectToString;
|
|
163
|
+
exports.remove = remove;
|
|
164
|
+
exports.toHandlerKey = toHandlerKey;
|
|
165
|
+
exports.toNumber = toNumber;
|
|
166
|
+
exports.toRawType = toRawType;
|
|
167
|
+
exports.toTypeString = toTypeString;
|
package/dist/shared.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Make a map and return a function for checking if a key
|
|
3
|
+
* is in that map.
|
|
4
|
+
* IMPORTANT: all calls of this function must be prefixed with
|
|
5
|
+
* \/\*#\_\_PURE\_\_\*\/
|
|
6
|
+
* So that rollup can tree-shake them if necessary.
|
|
7
|
+
*/
|
|
8
|
+
export declare function makeMap(str: string): (key: string) => boolean;
|
|
9
|
+
|
|
10
|
+
export declare const EMPTY_OBJ: {
|
|
11
|
+
readonly [key: string]: any;
|
|
12
|
+
};
|
|
13
|
+
export declare const EMPTY_ARR: readonly never[];
|
|
14
|
+
export declare const NOOP: () => void;
|
|
15
|
+
/**
|
|
16
|
+
* Always return false.
|
|
17
|
+
*/
|
|
18
|
+
export declare const NO: () => boolean;
|
|
19
|
+
export declare const isOn: (key: string) => boolean;
|
|
20
|
+
export declare const isModelListener: (key: string) => key is `onUpdate:${string}`;
|
|
21
|
+
export declare const extend: typeof Object.assign;
|
|
22
|
+
export declare const remove: <T>(arr: T[], el: T) => void;
|
|
23
|
+
export declare const hasOwn: (val: object, key: string | symbol) => key is keyof typeof val;
|
|
24
|
+
export declare const isArray: typeof Array.isArray;
|
|
25
|
+
export declare const isMap: (val: unknown) => val is Map<any, any>;
|
|
26
|
+
export declare const isSet: (val: unknown) => val is Set<any>;
|
|
27
|
+
export declare const isDate: (val: unknown) => val is Date;
|
|
28
|
+
export declare const isRegExp: (val: unknown) => val is RegExp;
|
|
29
|
+
export declare const isFunction: (val: unknown) => val is Function;
|
|
30
|
+
export declare const isString: (val: unknown) => val is string;
|
|
31
|
+
export declare const isSymbol: (val: unknown) => val is symbol;
|
|
32
|
+
export declare const isObject: (val: unknown) => val is Record<any, any>;
|
|
33
|
+
export declare const isPromise: <T = any>(val: unknown) => val is Promise<T>;
|
|
34
|
+
export declare const objectToString: typeof Object.prototype.toString;
|
|
35
|
+
export declare const toTypeString: (value: unknown) => string;
|
|
36
|
+
export declare const toRawType: (value: unknown) => string;
|
|
37
|
+
export declare const isPlainObject: (val: unknown) => val is object;
|
|
38
|
+
export declare const isIntegerKey: (key: unknown) => boolean;
|
|
39
|
+
/**
|
|
40
|
+
* @private
|
|
41
|
+
*/
|
|
42
|
+
export declare const camelize: (str: string) => string;
|
|
43
|
+
/**
|
|
44
|
+
* @private
|
|
45
|
+
*/
|
|
46
|
+
export declare const hyphenate: (str: string) => string;
|
|
47
|
+
/**
|
|
48
|
+
* @private
|
|
49
|
+
*/
|
|
50
|
+
export declare const capitalize: <T extends string>(str: T) => Capitalize<T>;
|
|
51
|
+
/**
|
|
52
|
+
* @private
|
|
53
|
+
*/
|
|
54
|
+
export declare const toHandlerKey: <T extends string>(str: T) => T extends '' ? '' : `on${Capitalize<T>}`;
|
|
55
|
+
export declare const hasChanged: (value: any, oldValue: any) => boolean;
|
|
56
|
+
export declare const invokeArrayFns: (fns: Function[], ...arg: any[]) => void;
|
|
57
|
+
export declare const def: (obj: object, key: string | symbol, value: any, writable?: boolean) => void;
|
|
58
|
+
/**
|
|
59
|
+
* "123-foo" will be parsed to 123
|
|
60
|
+
* This is used for the .number modifier in v-model
|
|
61
|
+
*/
|
|
62
|
+
export declare const looseToNumber: (val: any) => any;
|
|
63
|
+
/**
|
|
64
|
+
* Only concerns number-like strings
|
|
65
|
+
* "123-foo" will be returned as-is
|
|
66
|
+
*/
|
|
67
|
+
export declare const toNumber: (val: any) => any;
|
|
68
|
+
export declare const getGlobalThis: () => any;
|
|
69
|
+
export declare function genPropsAccessExp(name: string): string;
|
|
70
|
+
export declare function genCacheKey(source: string, options: any): string;
|
|
71
|
+
|
|
72
|
+
export type Prettify<T> = {
|
|
73
|
+
[K in keyof T]: T[K];
|
|
74
|
+
} & {};
|
|
75
|
+
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
76
|
+
export type LooseRequired<T> = {
|
|
77
|
+
[P in keyof (T & Required<T>)]: T[P];
|
|
78
|
+
};
|
|
79
|
+
export type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N;
|
|
80
|
+
export type IsKeyValues<T, K = string> = IfAny<T, false, T extends object ? (keyof T extends K ? true : false) : false>;
|
|
81
|
+
/**
|
|
82
|
+
* Utility for extracting the parameters from a function overload (for typed emits)
|
|
83
|
+
* https://github.com/microsoft/TypeScript/issues/32164#issuecomment-1146737709
|
|
84
|
+
*/
|
|
85
|
+
export type OverloadParameters<T extends (...args: any[]) => any> = Parameters<OverloadUnion<T>>;
|
|
86
|
+
type OverloadProps<TOverload> = Pick<TOverload, keyof TOverload>;
|
|
87
|
+
type OverloadUnionRecursive<TOverload, TPartialOverload = unknown> = TOverload extends (...args: infer TArgs) => infer TReturn ? TPartialOverload extends TOverload ? never : OverloadUnionRecursive<TPartialOverload & TOverload, TPartialOverload & ((...args: TArgs) => TReturn) & OverloadProps<TOverload>> | ((...args: TArgs) => TReturn) : never;
|
|
88
|
+
type OverloadUnion<TOverload extends (...args: any[]) => any> = Exclude<OverloadUnionRecursive<(() => never) & TOverload>, TOverload extends () => never ? never : () => never>;
|
|
89
|
+
|
|
90
|
+
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* shared v0.1.0-beta.0
|
|
3
|
+
* (c) 2026 baicie
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
**/
|
|
6
|
+
//#region packages/core/shared/src/makeMap.ts
|
|
7
|
+
/**
|
|
8
|
+
* Make a map and return a function for checking if a key
|
|
9
|
+
* is in that map.
|
|
10
|
+
* IMPORTANT: all calls of this function must be prefixed with
|
|
11
|
+
* \/\*#\_\_PURE\_\_\*\/
|
|
12
|
+
* So that rollup can tree-shake them if necessary.
|
|
13
|
+
*/
|
|
14
|
+
/*@__NO_SIDE_EFFECTS__*/
|
|
15
|
+
function makeMap(str) {
|
|
16
|
+
const map = Object.create(null);
|
|
17
|
+
for (const key of str.split(",")) map[key] = 1;
|
|
18
|
+
return (val) => val in map;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region packages/core/shared/src/general.ts
|
|
22
|
+
const EMPTY_OBJ = Object.freeze({});
|
|
23
|
+
const EMPTY_ARR = Object.freeze([]);
|
|
24
|
+
const NOOP = () => {};
|
|
25
|
+
/**
|
|
26
|
+
* Always return false.
|
|
27
|
+
*/
|
|
28
|
+
const NO = () => false;
|
|
29
|
+
const isOn = (key) => key.charCodeAt(0) === 111 && key.charCodeAt(1) === 110 && (key.charCodeAt(2) > 122 || key.charCodeAt(2) < 97);
|
|
30
|
+
const isModelListener = (key) => key.startsWith("onUpdate:");
|
|
31
|
+
const extend = Object.assign;
|
|
32
|
+
const remove = (arr, el) => {
|
|
33
|
+
const i = arr.indexOf(el);
|
|
34
|
+
if (i > -1) arr.splice(i, 1);
|
|
35
|
+
};
|
|
36
|
+
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
37
|
+
const hasOwn = (val, key) => hasOwnProperty.call(val, key);
|
|
38
|
+
const isArray = Array.isArray;
|
|
39
|
+
const isMap = (val) => toTypeString(val) === "[object Map]";
|
|
40
|
+
const isSet = (val) => toTypeString(val) === "[object Set]";
|
|
41
|
+
const isDate = (val) => toTypeString(val) === "[object Date]";
|
|
42
|
+
const isRegExp = (val) => toTypeString(val) === "[object RegExp]";
|
|
43
|
+
const isFunction = (val) => typeof val === "function";
|
|
44
|
+
const isString = (val) => typeof val === "string";
|
|
45
|
+
const isSymbol = (val) => typeof val === "symbol";
|
|
46
|
+
const isObject = (val) => val !== null && typeof val === "object";
|
|
47
|
+
const isPromise = (val) => {
|
|
48
|
+
return (isObject(val) || isFunction(val)) && isFunction(val.then) && isFunction(val.catch);
|
|
49
|
+
};
|
|
50
|
+
const objectToString = Object.prototype.toString;
|
|
51
|
+
const toTypeString = (value) => objectToString.call(value);
|
|
52
|
+
const toRawType = (value) => {
|
|
53
|
+
return toTypeString(value).slice(8, -1);
|
|
54
|
+
};
|
|
55
|
+
const isPlainObject = (val) => toTypeString(val) === "[object Object]";
|
|
56
|
+
const isIntegerKey = (key) => isString(key) && key !== "NaN" && key[0] !== "-" && "" + parseInt(key, 10) === key;
|
|
57
|
+
const cacheStringFunction = (fn) => {
|
|
58
|
+
const cache = Object.create(null);
|
|
59
|
+
return ((str) => {
|
|
60
|
+
return cache[str] || (cache[str] = fn(str));
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
const camelizeRE = /-\w/g;
|
|
64
|
+
/**
|
|
65
|
+
* @private
|
|
66
|
+
*/
|
|
67
|
+
const camelize = cacheStringFunction((str) => {
|
|
68
|
+
return str.replace(camelizeRE, (c) => c.slice(1).toUpperCase());
|
|
69
|
+
});
|
|
70
|
+
const hyphenateRE = /\B([A-Z])/g;
|
|
71
|
+
/**
|
|
72
|
+
* @private
|
|
73
|
+
*/
|
|
74
|
+
const hyphenate = cacheStringFunction((str) => str.replace(hyphenateRE, "-$1").toLowerCase());
|
|
75
|
+
/**
|
|
76
|
+
* @private
|
|
77
|
+
*/
|
|
78
|
+
const capitalize = cacheStringFunction((str) => {
|
|
79
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
80
|
+
});
|
|
81
|
+
/**
|
|
82
|
+
* @private
|
|
83
|
+
*/
|
|
84
|
+
const toHandlerKey = cacheStringFunction((str) => {
|
|
85
|
+
return str ? `on${capitalize(str)}` : ``;
|
|
86
|
+
});
|
|
87
|
+
const hasChanged = (value, oldValue) => !Object.is(value, oldValue);
|
|
88
|
+
const invokeArrayFns = (fns, ...arg) => {
|
|
89
|
+
for (let i = 0; i < fns.length; i++) fns[i](...arg);
|
|
90
|
+
};
|
|
91
|
+
const def = (obj, key, value, writable = false) => {
|
|
92
|
+
Object.defineProperty(obj, key, {
|
|
93
|
+
configurable: true,
|
|
94
|
+
enumerable: false,
|
|
95
|
+
writable,
|
|
96
|
+
value
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* "123-foo" will be parsed to 123
|
|
101
|
+
* This is used for the .number modifier in v-model
|
|
102
|
+
*/
|
|
103
|
+
const looseToNumber = (val) => {
|
|
104
|
+
const n = parseFloat(val);
|
|
105
|
+
return isNaN(n) ? val : n;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Only concerns number-like strings
|
|
109
|
+
* "123-foo" will be returned as-is
|
|
110
|
+
*/
|
|
111
|
+
const toNumber = (val) => {
|
|
112
|
+
const n = isString(val) ? Number(val) : NaN;
|
|
113
|
+
return isNaN(n) ? val : n;
|
|
114
|
+
};
|
|
115
|
+
let _globalThis;
|
|
116
|
+
const getGlobalThis = () => {
|
|
117
|
+
return _globalThis || (_globalThis = typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {});
|
|
118
|
+
};
|
|
119
|
+
const identRE = /^[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*$/;
|
|
120
|
+
function genPropsAccessExp(name) {
|
|
121
|
+
return identRE.test(name) ? `__props.${name}` : `__props[${JSON.stringify(name)}]`;
|
|
122
|
+
}
|
|
123
|
+
function genCacheKey(source, options) {
|
|
124
|
+
return source + JSON.stringify(options, (_, val) => typeof val === "function" ? val.toString() : val);
|
|
125
|
+
}
|
|
126
|
+
//#endregion
|
|
127
|
+
export { EMPTY_ARR, EMPTY_OBJ, NO, NOOP, camelize, capitalize, def, extend, genCacheKey, genPropsAccessExp, getGlobalThis, hasChanged, hasOwn, hyphenate, invokeArrayFns, isArray, isDate, isFunction, isIntegerKey, isMap, isModelListener, isObject, isOn, isPlainObject, isPromise, isRegExp, isSet, isString, isSymbol, looseToNumber, makeMap, objectToString, remove, toHandlerKey, toNumber, toRawType, toTypeString };
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zeus-js/shared",
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "internal utils shared across @zeus-js packages",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"module": "dist/shared.esm-bundler.js",
|
|
7
|
+
"types": "dist/shared.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"index.js",
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/shared.d.ts",
|
|
15
|
+
"node": {
|
|
16
|
+
"production": "./dist/shared.cjs.prod.js",
|
|
17
|
+
"development": "./dist/shared.cjs.js",
|
|
18
|
+
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"module": "./dist/shared.esm-bundler.js",
|
|
21
|
+
"import": "./dist/shared.esm-bundler.js",
|
|
22
|
+
"require": "./index.js"
|
|
23
|
+
},
|
|
24
|
+
"./*": "./*"
|
|
25
|
+
},
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"buildOptions": {
|
|
28
|
+
"formats": [
|
|
29
|
+
"esm-bundler",
|
|
30
|
+
"cjs"
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/baicie/zeus.git",
|
|
36
|
+
"directory": "packages/core/shared"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"zeus"
|
|
40
|
+
],
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/baicie/zeus/issues"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/baicie/zeus/tree/main/packages/core/shared#readme"
|
|
46
|
+
}
|