@hypernym/utils 0.3.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/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) Hypernym Studio
3
+ Copyright (c) 2023 Hypernym Studio, Ivo Dolenc
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Utils
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
- - Zero dependencies
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
- ## Imports
20
+ ## Usage
19
21
 
20
22
  ```ts
23
+ // CJS
24
+ const { isNull, isString, ... } = require('@hypernym/utils')
25
+
21
26
  // ESM & TS
22
- import { util } from '@hypernym/utils'
27
+ import { isNull, isString, ... } from '@hypernym/utils'
23
28
 
24
29
  // Types
25
- import type { Util } from '@hypernym/utils'
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 isClient: boolean;
14
+ declare const isBrowser: boolean;
15
15
  /**
16
16
  * Returns a boolean if the given value is a `null`.
17
17
  */
@@ -171,13 +171,13 @@ type RequiredObjectDeep<T extends object, Options extends OptionsDeep = {
171
171
  [K in keyof T]-?: RequiredDeep<T[K], Options>;
172
172
  };
173
173
 
174
- /**
175
- * Returns a high resolution timestamp in milliseconds using the `performance.now()` method.
176
- */
177
- declare const now: number;
178
174
  /**
179
175
  * An empty arrow function that performs no operation.
180
176
  */
181
177
  declare const noop: () => void;
178
+ /**
179
+ * Returns a string representing the object.
180
+ */
181
+ declare const toString: (v: any) => string;
182
182
 
183
- export { BuiltIn, IsAny, IsNever, IsNull, PartialDeep, Primitive, RequiredDeep, isArray, isArrayEmpty, isBigint, isBoolean, isClient, isDate, isElement, isError, isFunction, isHtmlCollection, isHtmlCollectionEmpty, isInfinity, isMap, isNaNValue, isNodeList, isNodeListEmpty, isNull, isNumber, isObject, isObjectEmpty, isPrimitive, isRegExp, isSet, isString, isStringEmpty, isSymbol, isURL, isUndefined, noop, now };
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,9 +1,8 @@
1
- const now = performance.now();
2
1
  const noop = () => {
3
2
  };
3
+ const toString = (v) => Object.prototype.toString.call(v).slice(8, -1);
4
4
 
5
- const getType = (v) => Object.prototype.toString.call(v).slice(8, -1);
6
- const isClient = typeof window !== "undefined";
5
+ const isBrowser = typeof window !== "undefined";
7
6
  const isNull = (v) => v === null;
8
7
  const isUndefined = (v) => typeof v === "undefined";
9
8
  const isString = (v) => typeof v === "string";
@@ -12,14 +11,14 @@ const isBoolean = (v) => typeof v === "boolean";
12
11
  const isNumber = (v) => typeof v === "number" && !isNaN(v);
13
12
  const isArray = (v) => Array.isArray(v);
14
13
  const isArrayEmpty = (v) => isArray(v) && v.length === 0;
15
- const isObject = (v) => getType(v) === "Object";
14
+ const isObject = (v) => toString(v) === "Object";
16
15
  const isObjectEmpty = (v) => isObject(v) && Object.keys(v).length === 0;
17
16
  const isFunction = (v) => v instanceof Function;
18
17
  const isNaNValue = (v) => typeof v === "number" && isNaN(v);
19
18
  const isRegExp = (v) => v instanceof RegExp;
20
19
  const isMap = (v) => v instanceof Map;
21
20
  const isSet = (v) => v instanceof Set;
22
- const isSymbol = (v) => getType(v) === "Symbol";
21
+ const isSymbol = (v) => toString(v) === "Symbol";
23
22
  const isDate = (v) => v instanceof Date && !isNaN(v.valueOf());
24
23
  const isBigint = (v) => typeof v === "bigint";
25
24
  const isInfinity = (v) => v === Infinity || v === -Infinity;
@@ -32,4 +31,4 @@ const isNodeListEmpty = (v) => isNodeList(v) && v.length === 0;
32
31
  const isHtmlCollection = (v) => v instanceof HTMLCollection;
33
32
  const isHtmlCollectionEmpty = (v) => isHtmlCollection(v) && v.length === 0;
34
33
 
35
- export { isArray, isArrayEmpty, isBigint, isBoolean, isClient, isDate, isElement, isError, isFunction, isHtmlCollection, isHtmlCollectionEmpty, isInfinity, isMap, isNaNValue, isNodeList, isNodeListEmpty, isNull, isNumber, isObject, isObjectEmpty, isPrimitive, isRegExp, isSet, isString, isStringEmpty, isSymbol, isURL, isUndefined, noop, now };
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.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,26 +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",
39
+ "build": "rollup -c ./.config/rollup.config.js",
40
+ "test:types": "vitest -c ./.config/vitest.config.ts typecheck",
34
41
  "prepublishOnly": "npm run build",
35
42
  "format": "prettier --write .",
36
43
  "lint": "eslint .",
37
44
  "fix": "eslint --fix ."
38
45
  },
39
46
  "devDependencies": {
40
- "@types/node": "^20.2.5",
41
- "@typescript-eslint/eslint-plugin": "^5.59.8",
42
- "@typescript-eslint/parser": "^5.59.8",
43
- "eslint": "^8.41.0",
44
- "eslint-config-prettier": "^8.8.0",
45
- "expect-type": "^0.16.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.23.0",
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.31.4"
57
+ "vite-node": "^0.32.2",
58
+ "vitest": "^0.32.2"
53
59
  },
54
60
  "publishConfig": {
55
61
  "access": "public"
@@ -57,5 +63,5 @@
57
63
  "eslintConfig": {
58
64
  "extends": "./.config/eslint.config.cjs"
59
65
  },
60
- "prettier": "./.config/prettier.config.cjs"
66
+ "prettier": "@hypernym/prettier-config"
61
67
  }