@hypernym/utils 0.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.
- package/LICENSE.txt +21 -0
- package/README.md +52 -0
- package/dist/utils.d.ts +154 -0
- package/dist/utils.mjs +27 -0
- package/package.json +60 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Hypernym Studio
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Utils
|
|
2
|
+
|
|
3
|
+
A collection of reusable utilities.
|
|
4
|
+
|
|
5
|
+
<sub><a href="https://github.com/hypernym-studio/utils">Repository</a> | <a href="https://www.npmjs.com/package/@hypernym/utils">Package</a> | <a href="https://github.com/hypernym-studio/utils/releases">Releases</a> | <a href="https://github.com/hypernym-studio/utils/discussions">Discussions</a></sub>
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- TypeScript friendly
|
|
10
|
+
- Zero dependencies
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm i @hypernym/utils
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Imports
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
// ESM & TS
|
|
22
|
+
import { util } from '@hypernym/utils'
|
|
23
|
+
|
|
24
|
+
// Types
|
|
25
|
+
import type { Util } from '@hypernym/utils'
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## API
|
|
29
|
+
|
|
30
|
+
- [`is`](./src/is.ts) - Simple conditional utilities.
|
|
31
|
+
|
|
32
|
+
### Types
|
|
33
|
+
|
|
34
|
+
- [`Primitive`](./src/types/primitive.ts) - Matches any primitive value.
|
|
35
|
+
- [`BuiltIn`](./src/types/built-in.ts) - Matches any `Primitive`, `Date` or `RegExp` value.
|
|
36
|
+
- [`PartialDeep`](./src/types/partial-deep.ts) - Constructs a type by recursively setting all properties as optional.
|
|
37
|
+
- [`RequiredDeep`](./src/types/required-deep.ts) - Constructs a type by recursively setting all properties as required.
|
|
38
|
+
- [`IsNull`](./src/types/is.ts) - Returns a boolean if the given type is a `null`.
|
|
39
|
+
- [`IsAny`](./src/types/is.ts) - Returns a boolean if the given type is a `any`.
|
|
40
|
+
- [`IsNever`](./src/types/is.ts) - Returns a boolean if the given type is a `never`.
|
|
41
|
+
|
|
42
|
+
## Community
|
|
43
|
+
|
|
44
|
+
Feel free to use the official [discussions](https://github.com/hypernym-studio/utils/discussions) for any additional questions.
|
|
45
|
+
|
|
46
|
+
## License
|
|
47
|
+
|
|
48
|
+
Developed in ðŸ‡ðŸ‡· Croatia
|
|
49
|
+
|
|
50
|
+
Released under the [MIT](LICENSE.txt) license.
|
|
51
|
+
|
|
52
|
+
© Hypernym Studio
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Matches any primitive value.
|
|
3
|
+
*/
|
|
4
|
+
type Primitive = null | undefined | string | number | boolean | symbol | bigint;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Matches any `Primitive`, `Date` or `RegExp` value.
|
|
8
|
+
*/
|
|
9
|
+
type BuiltIn = Primitive | Date | RegExp;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Checks if the code is running in the browser.
|
|
13
|
+
*/
|
|
14
|
+
declare const isClient: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* An empty arrow function that performs no operation.
|
|
17
|
+
*/
|
|
18
|
+
declare const noop: () => void;
|
|
19
|
+
/**
|
|
20
|
+
* Returns a boolean if the given value is a `null`.
|
|
21
|
+
*/
|
|
22
|
+
declare const isNull: (v: any) => v is null;
|
|
23
|
+
/**
|
|
24
|
+
* Returns a boolean if the given value is a `undefined`.
|
|
25
|
+
*/
|
|
26
|
+
declare const isUndefined: (v: any) => v is undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Returns a boolean if the given value is a `string`.
|
|
29
|
+
*/
|
|
30
|
+
declare const isString: (v: any) => v is string;
|
|
31
|
+
/**
|
|
32
|
+
* Returns a boolean if the given value is an empty `string`.
|
|
33
|
+
*/
|
|
34
|
+
declare const isStringEmpty: (v: any) => v is string;
|
|
35
|
+
/**
|
|
36
|
+
* Returns a boolean if the given value is a `boolean`.
|
|
37
|
+
*/
|
|
38
|
+
declare const isBoolean: (v: any) => v is boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Returns a boolean if the given value is a `number`.
|
|
41
|
+
*/
|
|
42
|
+
declare const isNumber: (v: any) => v is number;
|
|
43
|
+
/**
|
|
44
|
+
* Returns a boolean if the given value is a `array`.
|
|
45
|
+
*/
|
|
46
|
+
declare const isArray: (v: any) => v is any[];
|
|
47
|
+
/**
|
|
48
|
+
* Returns a boolean if the given value is an empty `array`.
|
|
49
|
+
*/
|
|
50
|
+
declare const isArrayEmpty: (v: any) => v is any[];
|
|
51
|
+
/**
|
|
52
|
+
* Returns a boolean if the given value is a `object`.
|
|
53
|
+
*/
|
|
54
|
+
declare const isObject: (v: any) => v is object;
|
|
55
|
+
/**
|
|
56
|
+
* Returns a boolean if the given value is an empty `object`.
|
|
57
|
+
*/
|
|
58
|
+
declare const isObjectEmpty: (v: any) => v is object;
|
|
59
|
+
/**
|
|
60
|
+
* Returns a boolean if the given value is a `Function`.
|
|
61
|
+
*/
|
|
62
|
+
declare const isFunction: (v: any) => v is (...args: any[]) => unknown;
|
|
63
|
+
/**
|
|
64
|
+
* Returns a boolean if the given value is a `NaN`.
|
|
65
|
+
*/
|
|
66
|
+
declare const isNaNValue: (v: any) => v is number;
|
|
67
|
+
/**
|
|
68
|
+
* Returns a boolean if the given value is a `RegExp`.
|
|
69
|
+
*/
|
|
70
|
+
declare const isRegExp: (v: any) => v is RegExp;
|
|
71
|
+
/**
|
|
72
|
+
* Returns a boolean if the given value is a `map`.
|
|
73
|
+
*/
|
|
74
|
+
declare const isMap: (v: any) => v is Map<any, any>;
|
|
75
|
+
/**
|
|
76
|
+
* Returns a boolean if the given value is a `set`.
|
|
77
|
+
*/
|
|
78
|
+
declare const isSet: (v: any) => v is Set<any>;
|
|
79
|
+
/**
|
|
80
|
+
* Returns a boolean if the given value is a `symbol`.
|
|
81
|
+
*/
|
|
82
|
+
declare const isSymbol: (v: any) => v is symbol;
|
|
83
|
+
/**
|
|
84
|
+
* Returns a boolean if the given value is a `Date`.
|
|
85
|
+
*/
|
|
86
|
+
declare const isDate: (v: any) => v is Date;
|
|
87
|
+
/**
|
|
88
|
+
* Returns a boolean if the given value is a `bigint`.
|
|
89
|
+
*/
|
|
90
|
+
declare const isBigint: (v: any) => v is bigint;
|
|
91
|
+
/**
|
|
92
|
+
* Returns a boolean if the given value is a `Infinity`.
|
|
93
|
+
*/
|
|
94
|
+
declare const isInfinity: (v: any) => v is number;
|
|
95
|
+
/**
|
|
96
|
+
* Returns a boolean if the given value is a `URL`.
|
|
97
|
+
*/
|
|
98
|
+
declare const isURL: (v: any) => v is URL;
|
|
99
|
+
/**
|
|
100
|
+
* Returns a boolean if the given value is a `Error`.
|
|
101
|
+
*/
|
|
102
|
+
declare const isError: (v: any) => v is Error;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Returns a boolean if the given type is a `null`.
|
|
106
|
+
*/
|
|
107
|
+
type IsNull<T> = [T] extends [null] ? true : false;
|
|
108
|
+
/**
|
|
109
|
+
* Returns a boolean if the given type is a `any`.
|
|
110
|
+
*/
|
|
111
|
+
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
112
|
+
/**
|
|
113
|
+
* Returns a boolean if the given type is a `never`.
|
|
114
|
+
*/
|
|
115
|
+
type IsNever<T> = [T] extends [never] ? true : false;
|
|
116
|
+
|
|
117
|
+
type OptionsDeep = {
|
|
118
|
+
/**
|
|
119
|
+
* Enables recursive mode for arrays and tuples.
|
|
120
|
+
*
|
|
121
|
+
* @default true
|
|
122
|
+
*/
|
|
123
|
+
readonly arrays?: boolean;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Constructs a type by recursively setting all properties as optional.
|
|
128
|
+
*
|
|
129
|
+
* Use `Partial<T>` for one level.
|
|
130
|
+
*/
|
|
131
|
+
type PartialDeep<T, Options extends OptionsDeep = {
|
|
132
|
+
arrays: true;
|
|
133
|
+
}> = T extends BuiltIn ? T : T extends Map<infer K, infer V> ? Map<PartialDeep<K, Options>, PartialDeep<V, Options>> : T extends ReadonlyMap<infer K, infer V> ? ReadonlyMap<PartialDeep<K, Options>, PartialDeep<V, Options>> : T extends WeakMap<infer K, infer V> ? WeakMap<PartialDeep<K, Options>, PartialDeep<V, Options>> : T extends Set<infer V> ? Set<PartialDeep<V, Options>> : T extends ReadonlySet<infer V> ? ReadonlySet<PartialDeep<V, Options>> : T extends WeakSet<infer V> ? WeakSet<PartialDeep<V, Options>> : T extends Promise<infer V> ? Promise<PartialDeep<V, Options>> : T extends (...args: any[]) => unknown ? T | undefined : T extends object ? T extends ReadonlyArray<infer V> ? Options['arrays'] extends true ? V[] extends T ? readonly V[] extends T ? ReadonlyArray<PartialDeep<V | undefined, Options>> : Array<PartialDeep<V | undefined, Options>> : PartialObjectDeep<T, Options> : T : PartialObjectDeep<T, Options> : unknown;
|
|
134
|
+
type PartialObjectDeep<T extends object, Options extends OptionsDeep = {
|
|
135
|
+
arrays: true;
|
|
136
|
+
}> = {
|
|
137
|
+
[K in keyof T]?: PartialDeep<T[K], Options>;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Constructs a type by recursively setting all properties as required.
|
|
142
|
+
*
|
|
143
|
+
* Use `Required<T>` for one level.
|
|
144
|
+
*/
|
|
145
|
+
type RequiredDeep<T, Options extends OptionsDeep = {
|
|
146
|
+
arrays: true;
|
|
147
|
+
}> = T extends BuiltIn ? Exclude<T, undefined> : T extends Map<infer K, infer V> ? Map<RequiredDeep<K, Options>, RequiredDeep<V, Options>> : T extends ReadonlyMap<infer K, infer V> ? ReadonlyMap<RequiredDeep<K, Options>, RequiredDeep<V, Options>> : T extends WeakMap<infer K, infer V> ? WeakMap<RequiredDeep<K, Options>, RequiredDeep<V, Options>> : T extends Set<infer V> ? Set<RequiredDeep<V, Options>> : T extends ReadonlySet<infer V> ? ReadonlySet<RequiredDeep<V, Options>> : T extends WeakSet<infer V> ? WeakSet<RequiredDeep<V, Options>> : T extends Promise<infer V> ? Promise<RequiredDeep<V, Options>> : T extends (...args: any[]) => unknown ? Exclude<T, undefined> : T extends object ? T extends ReadonlyArray<infer V> ? Options['arrays'] extends true ? V[] extends T ? readonly V[] extends T ? ReadonlyArray<RequiredDeep<Exclude<V, undefined>, Options>> : Array<RequiredDeep<Exclude<V, undefined>, Options>> : RequiredObjectDeep<T, Options> : Exclude<T, undefined> : RequiredObjectDeep<T, Options> : unknown;
|
|
148
|
+
type RequiredObjectDeep<T extends object, Options extends OptionsDeep = {
|
|
149
|
+
arrays: true;
|
|
150
|
+
}> = {
|
|
151
|
+
[K in keyof T]-?: RequiredDeep<T[K], Options>;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
export { BuiltIn, IsAny, IsNever, IsNull, PartialDeep, Primitive, RequiredDeep, isArray, isArrayEmpty, isBigint, isBoolean, isClient, isDate, isError, isFunction, isInfinity, isMap, isNaNValue, isNull, isNumber, isObject, isObjectEmpty, isRegExp, isSet, isString, isStringEmpty, isSymbol, isURL, isUndefined, noop };
|
package/dist/utils.mjs
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const getType = (v) => Object.prototype.toString.call(v).slice(8, -1);
|
|
2
|
+
const isClient = typeof window !== "undefined";
|
|
3
|
+
const noop = () => {
|
|
4
|
+
};
|
|
5
|
+
const isNull = (v) => v === null;
|
|
6
|
+
const isUndefined = (v) => typeof v === "undefined";
|
|
7
|
+
const isString = (v) => typeof v === "string";
|
|
8
|
+
const isStringEmpty = (v) => isString(v) && v.trim().length === 0;
|
|
9
|
+
const isBoolean = (v) => typeof v === "boolean";
|
|
10
|
+
const isNumber = (v) => typeof v === "number" && !isNaN(v);
|
|
11
|
+
const isArray = (v) => Array.isArray(v);
|
|
12
|
+
const isArrayEmpty = (v) => isArray(v) && v.length === 0;
|
|
13
|
+
const isObject = (v) => getType(v) === "Object";
|
|
14
|
+
const isObjectEmpty = (v) => isObject(v) && Object.keys(v).length === 0;
|
|
15
|
+
const isFunction = (v) => v instanceof Function;
|
|
16
|
+
const isNaNValue = (v) => typeof v === "number" && isNaN(v);
|
|
17
|
+
const isRegExp = (v) => v instanceof RegExp;
|
|
18
|
+
const isMap = (v) => v instanceof Map;
|
|
19
|
+
const isSet = (v) => v instanceof Set;
|
|
20
|
+
const isSymbol = (v) => getType(v) === "Symbol";
|
|
21
|
+
const isDate = (v) => v instanceof Date && !isNaN(v.valueOf());
|
|
22
|
+
const isBigint = (v) => typeof v === "bigint";
|
|
23
|
+
const isInfinity = (v) => v === Infinity || v === -Infinity;
|
|
24
|
+
const isURL = (v) => v instanceof URL;
|
|
25
|
+
const isError = (v) => v instanceof Error;
|
|
26
|
+
|
|
27
|
+
export { isArray, isArrayEmpty, isBigint, isBoolean, isClient, isDate, isError, isFunction, isInfinity, isMap, isNaNValue, isNull, isNumber, isObject, isObjectEmpty, isRegExp, isSet, isString, isStringEmpty, isSymbol, isURL, isUndefined, noop };
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hypernym/utils",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"author": "Hypernym Studio",
|
|
5
|
+
"description": "A collection of reusable utilities.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": "hypernym-studio/utils",
|
|
8
|
+
"homepage": "https://github.com/hypernym-studio/utils",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"module": "./dist/utils.mjs",
|
|
11
|
+
"types": "./dist/utils.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/utils.d.ts",
|
|
15
|
+
"import": "./dist/utils.mjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"keywords": [
|
|
22
|
+
"typescript",
|
|
23
|
+
"collection",
|
|
24
|
+
"utilities",
|
|
25
|
+
"helpers",
|
|
26
|
+
"utils",
|
|
27
|
+
"types",
|
|
28
|
+
"kit"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"dev": "vite playgrounds/client",
|
|
32
|
+
"dev:node": "vite-node -w playgrounds/node/main.ts",
|
|
33
|
+
"build": "rollup -c",
|
|
34
|
+
"prepublishOnly": "npm run build",
|
|
35
|
+
"format": "prettier --write .",
|
|
36
|
+
"lint": "eslint .",
|
|
37
|
+
"fix": "eslint --fix ."
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/node": "^20.2.3",
|
|
41
|
+
"@typescript-eslint/eslint-plugin": "^5.59.5",
|
|
42
|
+
"@typescript-eslint/parser": "^5.59.5",
|
|
43
|
+
"eslint": "^8.41.0",
|
|
44
|
+
"eslint-config-prettier": "^8.8.0",
|
|
45
|
+
"prettier": "^2.8.8",
|
|
46
|
+
"rollup": "^3.23.0",
|
|
47
|
+
"rollup-plugin-dts": "^5.3.0",
|
|
48
|
+
"rollup-plugin-esbuild": "^5.0.0",
|
|
49
|
+
"typescript": "^5.0.4",
|
|
50
|
+
"vite": "^4.3.5",
|
|
51
|
+
"vite-node": "^0.31.1"
|
|
52
|
+
},
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public"
|
|
55
|
+
},
|
|
56
|
+
"eslintConfig": {
|
|
57
|
+
"extends": "./.config/eslint.config.cjs"
|
|
58
|
+
},
|
|
59
|
+
"prettier": "./.config/prettier.config.cjs"
|
|
60
|
+
}
|