@hypernym/utils 0.3.1 → 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/LICENSE.txt +1 -1
- package/README.md +11 -21
- package/dist/utils.cjs +65 -0
- package/dist/utils.d.ts +6 -2
- package/dist/utils.mjs +5 -5
- package/package.json +18 -13
package/LICENSE.txt
CHANGED
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @hypernym/utils
|
|
2
2
|
|
|
3
3
|
A collection of reusable utilities.
|
|
4
4
|
|
|
@@ -6,40 +6,30 @@ A collection of reusable utilities.
|
|
|
6
6
|
|
|
7
7
|
## Features
|
|
8
8
|
|
|
9
|
+
- Supports CJS & ESM
|
|
9
10
|
- TypeScript friendly
|
|
10
|
-
-
|
|
11
|
+
- Fully tree-shakeable
|
|
12
|
+
- No dependencies
|
|
11
13
|
|
|
12
14
|
## Installation
|
|
13
15
|
|
|
14
16
|
```sh
|
|
15
|
-
npm i @hypernym/utils
|
|
17
|
+
npm i -D @hypernym/utils
|
|
16
18
|
```
|
|
17
19
|
|
|
18
|
-
##
|
|
20
|
+
## Usage
|
|
19
21
|
|
|
20
22
|
```ts
|
|
23
|
+
// CJS
|
|
24
|
+
const { isNull, isString, ... } = require('@hypernym/utils')
|
|
25
|
+
|
|
21
26
|
// ESM & TS
|
|
22
|
-
import {
|
|
27
|
+
import { isNull, isString, ... } from '@hypernym/utils'
|
|
23
28
|
|
|
24
29
|
// Types
|
|
25
|
-
import type {
|
|
30
|
+
import type { IsAny, RequiredDeep, ... } from '@hypernym/utils'
|
|
26
31
|
```
|
|
27
32
|
|
|
28
|
-
## API
|
|
29
|
-
|
|
30
|
-
- [`generic`](./src/generic.ts) - Useful common utilities.
|
|
31
|
-
- [`is`](./src/is.ts) - Simple conditional utilities.
|
|
32
|
-
|
|
33
|
-
### Types
|
|
34
|
-
|
|
35
|
-
- [`Primitive`](./src/types/primitive.ts) - Matches any primitive value.
|
|
36
|
-
- [`BuiltIn`](./src/types/built-in.ts) - Matches any `Primitive`, `Date` or `RegExp` value.
|
|
37
|
-
- [`PartialDeep`](./src/types/partial-deep.ts) - Constructs a type by recursively setting all properties as optional.
|
|
38
|
-
- [`RequiredDeep`](./src/types/required-deep.ts) - Constructs a type by recursively setting all properties as required.
|
|
39
|
-
- [`IsNull`](./src/types/is.ts) - Returns a boolean if the given type is a `null`.
|
|
40
|
-
- [`IsAny`](./src/types/is.ts) - Returns a boolean if the given type is a `any`.
|
|
41
|
-
- [`IsNever`](./src/types/is.ts) - Returns a boolean if the given type is a `never`.
|
|
42
|
-
|
|
43
33
|
## Community
|
|
44
34
|
|
|
45
35
|
Feel free to use the official [discussions](https://github.com/hypernym-studio/utils/discussions) for any additional questions.
|
package/dist/utils.cjs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const noop = () => {
|
|
4
|
+
};
|
|
5
|
+
const toString = (v) => Object.prototype.toString.call(v).slice(8, -1);
|
|
6
|
+
|
|
7
|
+
const isBrowser = typeof window !== "undefined";
|
|
8
|
+
const isNull = (v) => v === null;
|
|
9
|
+
const isUndefined = (v) => typeof v === "undefined";
|
|
10
|
+
const isString = (v) => typeof v === "string";
|
|
11
|
+
const isStringEmpty = (v) => isString(v) && v.trim().length === 0;
|
|
12
|
+
const isBoolean = (v) => typeof v === "boolean";
|
|
13
|
+
const isNumber = (v) => typeof v === "number" && !isNaN(v);
|
|
14
|
+
const isArray = (v) => Array.isArray(v);
|
|
15
|
+
const isArrayEmpty = (v) => isArray(v) && v.length === 0;
|
|
16
|
+
const isObject = (v) => toString(v) === "Object";
|
|
17
|
+
const isObjectEmpty = (v) => isObject(v) && Object.keys(v).length === 0;
|
|
18
|
+
const isFunction = (v) => v instanceof Function;
|
|
19
|
+
const isNaNValue = (v) => typeof v === "number" && isNaN(v);
|
|
20
|
+
const isRegExp = (v) => v instanceof RegExp;
|
|
21
|
+
const isMap = (v) => v instanceof Map;
|
|
22
|
+
const isSet = (v) => v instanceof Set;
|
|
23
|
+
const isSymbol = (v) => toString(v) === "Symbol";
|
|
24
|
+
const isDate = (v) => v instanceof Date && !isNaN(v.valueOf());
|
|
25
|
+
const isBigint = (v) => typeof v === "bigint";
|
|
26
|
+
const isInfinity = (v) => v === Infinity || v === -Infinity;
|
|
27
|
+
const isURL = (v) => v instanceof URL;
|
|
28
|
+
const isError = (v) => v instanceof Error;
|
|
29
|
+
const isPrimitive = (v) => isString(v) || isNumber(v) || isBigint(v) || isBoolean(v) || isSymbol(v) || isNull(v) || isUndefined(v);
|
|
30
|
+
const isElement = (v) => v instanceof Element;
|
|
31
|
+
const isNodeList = (v) => v instanceof NodeList;
|
|
32
|
+
const isNodeListEmpty = (v) => isNodeList(v) && v.length === 0;
|
|
33
|
+
const isHtmlCollection = (v) => v instanceof HTMLCollection;
|
|
34
|
+
const isHtmlCollectionEmpty = (v) => isHtmlCollection(v) && v.length === 0;
|
|
35
|
+
|
|
36
|
+
exports.isArray = isArray;
|
|
37
|
+
exports.isArrayEmpty = isArrayEmpty;
|
|
38
|
+
exports.isBigint = isBigint;
|
|
39
|
+
exports.isBoolean = isBoolean;
|
|
40
|
+
exports.isBrowser = isBrowser;
|
|
41
|
+
exports.isDate = isDate;
|
|
42
|
+
exports.isElement = isElement;
|
|
43
|
+
exports.isError = isError;
|
|
44
|
+
exports.isFunction = isFunction;
|
|
45
|
+
exports.isHtmlCollection = isHtmlCollection;
|
|
46
|
+
exports.isHtmlCollectionEmpty = isHtmlCollectionEmpty;
|
|
47
|
+
exports.isInfinity = isInfinity;
|
|
48
|
+
exports.isMap = isMap;
|
|
49
|
+
exports.isNaNValue = isNaNValue;
|
|
50
|
+
exports.isNodeList = isNodeList;
|
|
51
|
+
exports.isNodeListEmpty = isNodeListEmpty;
|
|
52
|
+
exports.isNull = isNull;
|
|
53
|
+
exports.isNumber = isNumber;
|
|
54
|
+
exports.isObject = isObject;
|
|
55
|
+
exports.isObjectEmpty = isObjectEmpty;
|
|
56
|
+
exports.isPrimitive = isPrimitive;
|
|
57
|
+
exports.isRegExp = isRegExp;
|
|
58
|
+
exports.isSet = isSet;
|
|
59
|
+
exports.isString = isString;
|
|
60
|
+
exports.isStringEmpty = isStringEmpty;
|
|
61
|
+
exports.isSymbol = isSymbol;
|
|
62
|
+
exports.isURL = isURL;
|
|
63
|
+
exports.isUndefined = isUndefined;
|
|
64
|
+
exports.noop = noop;
|
|
65
|
+
exports.toString = toString;
|
package/dist/utils.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ type BuiltIn = Primitive | Date | RegExp;
|
|
|
11
11
|
/**
|
|
12
12
|
* Checks if the code is running in the browser.
|
|
13
13
|
*/
|
|
14
|
-
declare const
|
|
14
|
+
declare const isBrowser: boolean;
|
|
15
15
|
/**
|
|
16
16
|
* Returns a boolean if the given value is a `null`.
|
|
17
17
|
*/
|
|
@@ -175,5 +175,9 @@ type RequiredObjectDeep<T extends object, Options extends OptionsDeep = {
|
|
|
175
175
|
* An empty arrow function that performs no operation.
|
|
176
176
|
*/
|
|
177
177
|
declare const noop: () => void;
|
|
178
|
+
/**
|
|
179
|
+
* Returns a string representing the object.
|
|
180
|
+
*/
|
|
181
|
+
declare const toString: (v: any) => string;
|
|
178
182
|
|
|
179
|
-
export { BuiltIn, IsAny, IsNever, IsNull, PartialDeep, Primitive, RequiredDeep, isArray, isArrayEmpty, isBigint, isBoolean,
|
|
183
|
+
export { BuiltIn, IsAny, IsNever, IsNull, PartialDeep, Primitive, RequiredDeep, isArray, isArrayEmpty, isBigint, isBoolean, isBrowser, isDate, isElement, isError, isFunction, isHtmlCollection, isHtmlCollectionEmpty, isInfinity, isMap, isNaNValue, isNodeList, isNodeListEmpty, isNull, isNumber, isObject, isObjectEmpty, isPrimitive, isRegExp, isSet, isString, isStringEmpty, isSymbol, isURL, isUndefined, noop, toString };
|
package/dist/utils.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const noop = () => {
|
|
2
2
|
};
|
|
3
|
+
const toString = (v) => Object.prototype.toString.call(v).slice(8, -1);
|
|
3
4
|
|
|
4
|
-
const
|
|
5
|
-
const isClient = typeof window !== "undefined";
|
|
5
|
+
const isBrowser = typeof window !== "undefined";
|
|
6
6
|
const isNull = (v) => v === null;
|
|
7
7
|
const isUndefined = (v) => typeof v === "undefined";
|
|
8
8
|
const isString = (v) => typeof v === "string";
|
|
@@ -11,14 +11,14 @@ const isBoolean = (v) => typeof v === "boolean";
|
|
|
11
11
|
const isNumber = (v) => typeof v === "number" && !isNaN(v);
|
|
12
12
|
const isArray = (v) => Array.isArray(v);
|
|
13
13
|
const isArrayEmpty = (v) => isArray(v) && v.length === 0;
|
|
14
|
-
const isObject = (v) =>
|
|
14
|
+
const isObject = (v) => toString(v) === "Object";
|
|
15
15
|
const isObjectEmpty = (v) => isObject(v) && Object.keys(v).length === 0;
|
|
16
16
|
const isFunction = (v) => v instanceof Function;
|
|
17
17
|
const isNaNValue = (v) => typeof v === "number" && isNaN(v);
|
|
18
18
|
const isRegExp = (v) => v instanceof RegExp;
|
|
19
19
|
const isMap = (v) => v instanceof Map;
|
|
20
20
|
const isSet = (v) => v instanceof Set;
|
|
21
|
-
const isSymbol = (v) =>
|
|
21
|
+
const isSymbol = (v) => toString(v) === "Symbol";
|
|
22
22
|
const isDate = (v) => v instanceof Date && !isNaN(v.valueOf());
|
|
23
23
|
const isBigint = (v) => typeof v === "bigint";
|
|
24
24
|
const isInfinity = (v) => v === Infinity || v === -Infinity;
|
|
@@ -31,4 +31,4 @@ const isNodeListEmpty = (v) => isNodeList(v) && v.length === 0;
|
|
|
31
31
|
const isHtmlCollection = (v) => v instanceof HTMLCollection;
|
|
32
32
|
const isHtmlCollectionEmpty = (v) => isHtmlCollection(v) && v.length === 0;
|
|
33
33
|
|
|
34
|
-
export { isArray, isArrayEmpty, isBigint, isBoolean,
|
|
34
|
+
export { isArray, isArrayEmpty, isBigint, isBoolean, isBrowser, isDate, isElement, isError, isFunction, isHtmlCollection, isHtmlCollectionEmpty, isInfinity, isMap, isNaNValue, isNodeList, isNodeListEmpty, isNull, isNumber, isObject, isObjectEmpty, isPrimitive, isRegExp, isSet, isString, isStringEmpty, isSymbol, isURL, isUndefined, noop, toString };
|
package/package.json
CHANGED
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hypernym/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"author": "Hypernym Studio",
|
|
5
|
+
"maintainers": [
|
|
6
|
+
"Ivo Dolenc (https://github.com/ivodolenc)"
|
|
7
|
+
],
|
|
5
8
|
"description": "A collection of reusable utilities.",
|
|
6
9
|
"license": "MIT",
|
|
10
|
+
"funding": "https://github.com/sponsors/ivodolenc",
|
|
7
11
|
"repository": "hypernym-studio/utils",
|
|
8
12
|
"homepage": "https://github.com/hypernym-studio/utils",
|
|
9
13
|
"type": "module",
|
|
14
|
+
"main": "./dist/utils.cjs",
|
|
10
15
|
"module": "./dist/utils.mjs",
|
|
11
16
|
"types": "./dist/utils.d.ts",
|
|
12
17
|
"exports": {
|
|
13
18
|
".": {
|
|
14
19
|
"types": "./dist/utils.d.ts",
|
|
15
|
-
"import": "./dist/utils.mjs"
|
|
20
|
+
"import": "./dist/utils.mjs",
|
|
21
|
+
"require": "./dist/utils.cjs"
|
|
16
22
|
}
|
|
17
23
|
},
|
|
18
24
|
"files": [
|
|
@@ -30,27 +36,26 @@
|
|
|
30
36
|
"scripts": {
|
|
31
37
|
"dev": "vite playgrounds/client",
|
|
32
38
|
"dev:node": "vite-node -w playgrounds/node/main.ts",
|
|
33
|
-
"build": "rollup -c",
|
|
34
|
-
"test:types": "vitest typecheck",
|
|
39
|
+
"build": "rollup -c ./.config/rollup.config.js",
|
|
40
|
+
"test:types": "vitest -c ./.config/vitest.config.ts typecheck",
|
|
35
41
|
"prepublishOnly": "npm run build",
|
|
36
42
|
"format": "prettier --write .",
|
|
37
43
|
"lint": "eslint .",
|
|
38
44
|
"fix": "eslint --fix ."
|
|
39
45
|
},
|
|
40
46
|
"devDependencies": {
|
|
41
|
-
"@
|
|
42
|
-
"@
|
|
43
|
-
"@
|
|
44
|
-
"eslint": "^8.
|
|
45
|
-
"eslint-config-prettier": "^8.8.0",
|
|
47
|
+
"@hypernym/eslint-config": "^1.0.1",
|
|
48
|
+
"@hypernym/prettier-config": "^1.0.1",
|
|
49
|
+
"@types/node": "^20.3.3",
|
|
50
|
+
"eslint": "^8.44.0",
|
|
46
51
|
"prettier": "^2.8.8",
|
|
47
|
-
"rollup": "^3.
|
|
52
|
+
"rollup": "^3.26.0",
|
|
48
53
|
"rollup-plugin-dts": "^5.3.0",
|
|
49
54
|
"rollup-plugin-esbuild": "^5.0.0",
|
|
50
55
|
"typescript": "^5.0.4",
|
|
51
56
|
"vite": "^4.3.9",
|
|
52
|
-
"vite-node": "^0.32.
|
|
53
|
-
"vitest": "^0.32.
|
|
57
|
+
"vite-node": "^0.32.2",
|
|
58
|
+
"vitest": "^0.32.2"
|
|
54
59
|
},
|
|
55
60
|
"publishConfig": {
|
|
56
61
|
"access": "public"
|
|
@@ -58,5 +63,5 @@
|
|
|
58
63
|
"eslintConfig": {
|
|
59
64
|
"extends": "./.config/eslint.config.cjs"
|
|
60
65
|
},
|
|
61
|
-
"prettier": "
|
|
66
|
+
"prettier": "@hypernym/prettier-config"
|
|
62
67
|
}
|