@hypernym/utils 0.3.1 → 1.1.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/node.cjs ADDED
@@ -0,0 +1,65 @@
1
+ 'use strict';
2
+
3
+ var promises = require('node:fs/promises');
4
+ var node_path = require('node:path');
5
+ var formatBytes = require('../mix/format-bytes');
6
+
7
+ async function exists(path) {
8
+ return await promises.access(path, promises.constants.F_OK).then(() => true).catch(() => false);
9
+ }
10
+
11
+ async function getDirStats(dirPath, options) {
12
+ const dirFiles = await promises.readdir(dirPath);
13
+ const dirBase = node_path.basename(dirPath);
14
+ let dirStats = [];
15
+ const subdirList = [];
16
+ const fileList = [];
17
+ let dirSize = 0;
18
+ let dirIndex = 0;
19
+ let fileIndex = -1;
20
+ let subdirIndex = -1;
21
+ for (const file of dirFiles) {
22
+ const filePath = node_path.resolve(dirPath, file);
23
+ const fileStat = await promises.stat(filePath);
24
+ if (fileStat.isDirectory()) {
25
+ subdirIndex++;
26
+ subdirList.push({
27
+ index: subdirIndex,
28
+ path: filePath,
29
+ base: file
30
+ });
31
+ if (options?.recursive) {
32
+ dirBase === dirPath ? dirBase : node_path.basename(filePath);
33
+ const stats = await getDirStats(filePath);
34
+ const updateDirStats = { ...stats[0], ...{ index: dirIndex++ } };
35
+ dirStats = [...dirStats, updateDirStats];
36
+ }
37
+ } else {
38
+ const { base, name, ext } = node_path.parse(file);
39
+ const path = filePath;
40
+ const size = formatBytes.formatBytes(fileStat.size);
41
+ fileIndex++;
42
+ dirSize += fileStat.size;
43
+ fileList.push({
44
+ index: fileIndex,
45
+ path,
46
+ base,
47
+ name,
48
+ ext,
49
+ size
50
+ });
51
+ }
52
+ }
53
+ dirStats.push({
54
+ index: dirIndex,
55
+ path: dirPath,
56
+ base: dirBase,
57
+ size: formatBytes.formatBytes(dirSize),
58
+ subdirs: subdirList,
59
+ files: fileList
60
+ });
61
+ return dirStats;
62
+ }
63
+
64
+ exports.exists = exists;
65
+ exports.getDirStats = getDirStats;
package/dist/node.d.ts ADDED
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Checks if the file exists.
3
+ */
4
+ declare function exists(path: string): Promise<boolean>;
5
+
6
+ /**
7
+ * Scans the specified directory and gets details for each subdirectory and file.
8
+ *
9
+ * By default, recursive mode is disabled so only one level is scanned.
10
+ */
11
+ declare function getDirStats(dirPath: string, options?: DirStatsOptions): Promise<DirStats[]>;
12
+
13
+ interface SubdirDetails {
14
+ index: number;
15
+ path: string;
16
+ base: string;
17
+ }
18
+ interface FileDetails {
19
+ index: number;
20
+ path: string;
21
+ base: string;
22
+ name: string;
23
+ ext: string;
24
+ size: string;
25
+ }
26
+ interface DirStats {
27
+ index: number;
28
+ path: string;
29
+ base: string;
30
+ size: string;
31
+ subdirs: SubdirDetails[];
32
+ files: FileDetails[];
33
+ }
34
+ interface DirStatsOptions {
35
+ recursive?: boolean;
36
+ }
37
+
38
+ export { DirStats, DirStatsOptions, exists, getDirStats };
package/dist/node.mjs ADDED
@@ -0,0 +1,62 @@
1
+ import { access, constants, readdir, stat } from 'node:fs/promises';
2
+ import { basename, resolve, parse } from 'node:path';
3
+ import { formatBytes } from '../mix/format-bytes';
4
+
5
+ async function exists(path) {
6
+ return await access(path, constants.F_OK).then(() => true).catch(() => false);
7
+ }
8
+
9
+ async function getDirStats(dirPath, options) {
10
+ const dirFiles = await readdir(dirPath);
11
+ const dirBase = basename(dirPath);
12
+ let dirStats = [];
13
+ const subdirList = [];
14
+ const fileList = [];
15
+ let dirSize = 0;
16
+ let dirIndex = 0;
17
+ let fileIndex = -1;
18
+ let subdirIndex = -1;
19
+ for (const file of dirFiles) {
20
+ const filePath = resolve(dirPath, file);
21
+ const fileStat = await stat(filePath);
22
+ if (fileStat.isDirectory()) {
23
+ subdirIndex++;
24
+ subdirList.push({
25
+ index: subdirIndex,
26
+ path: filePath,
27
+ base: file
28
+ });
29
+ if (options?.recursive) {
30
+ dirBase === dirPath ? dirBase : basename(filePath);
31
+ const stats = await getDirStats(filePath);
32
+ const updateDirStats = { ...stats[0], ...{ index: dirIndex++ } };
33
+ dirStats = [...dirStats, updateDirStats];
34
+ }
35
+ } else {
36
+ const { base, name, ext } = parse(file);
37
+ const path = filePath;
38
+ const size = formatBytes(fileStat.size);
39
+ fileIndex++;
40
+ dirSize += fileStat.size;
41
+ fileList.push({
42
+ index: fileIndex,
43
+ path,
44
+ base,
45
+ name,
46
+ ext,
47
+ size
48
+ });
49
+ }
50
+ }
51
+ dirStats.push({
52
+ index: dirIndex,
53
+ path: dirPath,
54
+ base: dirBase,
55
+ size: formatBytes(dirSize),
56
+ subdirs: subdirList,
57
+ files: fileList
58
+ });
59
+ return dirStats;
60
+ }
61
+
62
+ export { exists, getDirStats };
package/dist/utils.cjs ADDED
@@ -0,0 +1,77 @@
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
+ function formatBytes(bytes, options) {
37
+ const decimals = options?.decimals || 2;
38
+ const units = ["B", "KB", "MB", "GB", "TB"];
39
+ if (bytes === 0)
40
+ return `0 B`;
41
+ const k = 1024;
42
+ const dm = decimals < 0 ? 0 : decimals;
43
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
44
+ return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${units[i]}`;
45
+ }
46
+
47
+ exports.formatBytes = formatBytes;
48
+ exports.isArray = isArray;
49
+ exports.isArrayEmpty = isArrayEmpty;
50
+ exports.isBigint = isBigint;
51
+ exports.isBoolean = isBoolean;
52
+ exports.isBrowser = isBrowser;
53
+ exports.isDate = isDate;
54
+ exports.isElement = isElement;
55
+ exports.isError = isError;
56
+ exports.isFunction = isFunction;
57
+ exports.isHtmlCollection = isHtmlCollection;
58
+ exports.isHtmlCollectionEmpty = isHtmlCollectionEmpty;
59
+ exports.isInfinity = isInfinity;
60
+ exports.isMap = isMap;
61
+ exports.isNaNValue = isNaNValue;
62
+ exports.isNodeList = isNodeList;
63
+ exports.isNodeListEmpty = isNodeListEmpty;
64
+ exports.isNull = isNull;
65
+ exports.isNumber = isNumber;
66
+ exports.isObject = isObject;
67
+ exports.isObjectEmpty = isObjectEmpty;
68
+ exports.isPrimitive = isPrimitive;
69
+ exports.isRegExp = isRegExp;
70
+ exports.isSet = isSet;
71
+ exports.isString = isString;
72
+ exports.isStringEmpty = isStringEmpty;
73
+ exports.isSymbol = isSymbol;
74
+ exports.isURL = isURL;
75
+ exports.isUndefined = isUndefined;
76
+ exports.noop = noop;
77
+ exports.toString = toString;
package/dist/utils.d.ts CHANGED
@@ -1,8 +1,16 @@
1
+ /**
2
+ * An empty arrow function that performs no operation.
3
+ */
4
+ declare const noop: () => void;
5
+ /**
6
+ * Returns a string representing the object.
7
+ */
8
+ declare const toString: (v: any) => string;
9
+
1
10
  /**
2
11
  * Matches any primitive value.
3
12
  */
4
13
  type Primitive = null | undefined | string | number | boolean | symbol | bigint;
5
-
6
14
  /**
7
15
  * Matches any `Primitive`, `Date` or `RegExp` value.
8
16
  */
@@ -11,7 +19,7 @@ type BuiltIn = Primitive | Date | RegExp;
11
19
  /**
12
20
  * Checks if the code is running in the browser.
13
21
  */
14
- declare const isClient: boolean;
22
+ declare const isBrowser: boolean;
15
23
  /**
16
24
  * Returns a boolean if the given value is a `null`.
17
25
  */
@@ -134,6 +142,15 @@ type IsAny<T> = 0 extends 1 & T ? true : false;
134
142
  */
135
143
  type IsNever<T> = [T] extends [never] ? true : false;
136
144
 
145
+ /**
146
+ * Converts bytes to a _human-readable_ size and appends a units suffix.
147
+ */
148
+ declare function formatBytes(bytes: number, options?: FormatBytesOptions): string;
149
+
150
+ interface FormatBytesOptions {
151
+ decimals?: number;
152
+ }
153
+
137
154
  type OptionsDeep = {
138
155
  /**
139
156
  * Enables recursive mode for arrays and tuples.
@@ -171,9 +188,4 @@ type RequiredObjectDeep<T extends object, Options extends OptionsDeep = {
171
188
  [K in keyof T]-?: RequiredDeep<T[K], Options>;
172
189
  };
173
190
 
174
- /**
175
- * An empty arrow function that performs no operation.
176
- */
177
- declare const noop: () => void;
178
-
179
- 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 };
191
+ export { BuiltIn, FormatBytesOptions, IsAny, IsNever, IsNull, OptionsDeep, PartialDeep, Primitive, RequiredDeep, formatBytes, 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 getType = (v) => Object.prototype.toString.call(v).slice(8, -1);
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) => getType(v) === "Object";
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) => getType(v) === "Symbol";
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,15 @@ 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, 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 };
34
+ function formatBytes(bytes, options) {
35
+ const decimals = options?.decimals || 2;
36
+ const units = ["B", "KB", "MB", "GB", "TB"];
37
+ if (bytes === 0)
38
+ return `0 B`;
39
+ const k = 1024;
40
+ const dm = decimals < 0 ? 0 : decimals;
41
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
42
+ return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${units[i]}`;
43
+ }
44
+
45
+ export { formatBytes, 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/node.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/node'
package/package.json CHANGED
@@ -1,22 +1,34 @@
1
1
  {
2
2
  "name": "@hypernym/utils",
3
- "version": "0.3.1",
3
+ "version": "1.1.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"
22
+ },
23
+ "./node": {
24
+ "types": "./dist/node.d.ts",
25
+ "import": "./dist/node.mjs",
26
+ "require": "./dist/node.cjs"
16
27
  }
17
28
  },
18
29
  "files": [
19
- "dist"
30
+ "dist",
31
+ "*.d.ts"
20
32
  ],
21
33
  "keywords": [
22
34
  "typescript",
@@ -30,27 +42,26 @@
30
42
  "scripts": {
31
43
  "dev": "vite playgrounds/client",
32
44
  "dev:node": "vite-node -w playgrounds/node/main.ts",
33
- "build": "rollup -c",
34
- "test:types": "vitest typecheck",
45
+ "build": "rollup -c ./.config/rollup.config.js",
46
+ "test:types": "vitest -c ./.config/vitest.config.ts typecheck",
35
47
  "prepublishOnly": "npm run build",
36
48
  "format": "prettier --write .",
37
49
  "lint": "eslint .",
38
50
  "fix": "eslint --fix ."
39
51
  },
40
52
  "devDependencies": {
41
- "@types/node": "^20.2.5",
42
- "@typescript-eslint/eslint-plugin": "^5.59.9",
43
- "@typescript-eslint/parser": "^5.59.9",
44
- "eslint": "^8.42.0",
45
- "eslint-config-prettier": "^8.8.0",
53
+ "@hypernym/eslint-config": "^1.0.1",
54
+ "@hypernym/prettier-config": "^1.0.1",
55
+ "@types/node": "^20.4.1",
56
+ "eslint": "^8.44.0",
46
57
  "prettier": "^2.8.8",
47
- "rollup": "^3.24.0",
58
+ "rollup": "^3.26.2",
48
59
  "rollup-plugin-dts": "^5.3.0",
49
60
  "rollup-plugin-esbuild": "^5.0.0",
50
61
  "typescript": "^5.0.4",
51
- "vite": "^4.3.9",
52
- "vite-node": "^0.32.0",
53
- "vitest": "^0.32.0"
62
+ "vite": "^4.4.2",
63
+ "vite-node": "^0.33.0",
64
+ "vitest": "^0.33.0"
54
65
  },
55
66
  "publishConfig": {
56
67
  "access": "public"
@@ -58,5 +69,5 @@
58
69
  "eslintConfig": {
59
70
  "extends": "./.config/eslint.config.cjs"
60
71
  },
61
- "prettier": "./.config/prettier.config.cjs"
72
+ "prettier": "@hypernym/prettier-config"
62
73
  }