@hypernym/utils 1.0.0 → 1.1.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.
@@ -33,6 +33,18 @@ const isNodeListEmpty = (v) => isNodeList(v) && v.length === 0;
33
33
  const isHtmlCollection = (v) => v instanceof HTMLCollection;
34
34
  const isHtmlCollectionEmpty = (v) => isHtmlCollection(v) && v.length === 0;
35
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;
36
48
  exports.isArray = isArray;
37
49
  exports.isArrayEmpty = isArrayEmpty;
38
50
  exports.isBigint = isBigint;
@@ -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
  */
@@ -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,13 +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
- * Returns a string representing the object.
180
- */
181
- declare const toString: (v: any) => string;
182
-
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 };
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 };
@@ -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, 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 };
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 };
@@ -0,0 +1,63 @@
1
+ 'use strict';
2
+
3
+ var promises = require('node:fs/promises');
4
+ var node_path = require('node:path');
5
+
6
+ async function exists(path) {
7
+ return await promises.access(path, promises.constants.F_OK).then(() => true).catch(() => false);
8
+ }
9
+
10
+ async function getDirStats(path, options) {
11
+ const dirPath = node_path.resolve(path);
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(path, 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
+ const stats = await getDirStats(filePath);
33
+ const updateDirStats = { ...stats[0], ...{ index: dirIndex++ } };
34
+ dirStats = [...dirStats, updateDirStats];
35
+ }
36
+ } else {
37
+ const { base, name, ext } = node_path.parse(file);
38
+ const { size } = fileStat;
39
+ fileIndex++;
40
+ dirSize += size;
41
+ fileList.push({
42
+ index: fileIndex,
43
+ path: filePath,
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: dirSize,
56
+ subdirs: subdirList,
57
+ files: fileList
58
+ });
59
+ return dirStats;
60
+ }
61
+
62
+ exports.exists = exists;
63
+ exports.getDirStats = getDirStats;
@@ -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(path: 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: number;
25
+ }
26
+ interface DirStats {
27
+ index: number;
28
+ path: string;
29
+ base: string;
30
+ size: number;
31
+ subdirs: SubdirDetails[];
32
+ files: FileDetails[];
33
+ }
34
+ interface DirStatsOptions {
35
+ recursive?: boolean;
36
+ }
37
+
38
+ export { DirStats, DirStatsOptions, exists, getDirStats };
@@ -0,0 +1,60 @@
1
+ import { access, constants, readdir, stat } from 'node:fs/promises';
2
+ import { resolve, basename, parse } from 'node:path';
3
+
4
+ async function exists(path) {
5
+ return await access(path, constants.F_OK).then(() => true).catch(() => false);
6
+ }
7
+
8
+ async function getDirStats(path, options) {
9
+ const dirPath = resolve(path);
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(path, 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
+ const stats = await getDirStats(filePath);
31
+ const updateDirStats = { ...stats[0], ...{ index: dirIndex++ } };
32
+ dirStats = [...dirStats, updateDirStats];
33
+ }
34
+ } else {
35
+ const { base, name, ext } = parse(file);
36
+ const { size } = fileStat;
37
+ fileIndex++;
38
+ dirSize += size;
39
+ fileList.push({
40
+ index: fileIndex,
41
+ path: filePath,
42
+ base,
43
+ name,
44
+ ext,
45
+ size
46
+ });
47
+ }
48
+ }
49
+ dirStats.push({
50
+ index: dirIndex,
51
+ path: dirPath,
52
+ base: dirBase,
53
+ size: dirSize,
54
+ subdirs: subdirList,
55
+ files: fileList
56
+ });
57
+ return dirStats;
58
+ }
59
+
60
+ export { exists, getDirStats };
package/node.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/node'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hypernym/utils",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "author": "Hypernym Studio",
5
5
  "maintainers": [
6
6
  "Ivo Dolenc (https://github.com/ivodolenc)"
@@ -11,18 +11,24 @@
11
11
  "repository": "hypernym-studio/utils",
12
12
  "homepage": "https://github.com/hypernym-studio/utils",
13
13
  "type": "module",
14
- "main": "./dist/utils.cjs",
15
- "module": "./dist/utils.mjs",
16
- "types": "./dist/utils.d.ts",
14
+ "main": "./dist/index.cjs",
15
+ "module": "./dist/index.mjs",
16
+ "types": "./dist/index.d.ts",
17
17
  "exports": {
18
18
  ".": {
19
- "types": "./dist/utils.d.ts",
20
- "import": "./dist/utils.mjs",
21
- "require": "./dist/utils.cjs"
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.mjs",
21
+ "require": "./dist/index.cjs"
22
+ },
23
+ "./node": {
24
+ "types": "./dist/node/index.d.ts",
25
+ "import": "./dist/node/index.mjs",
26
+ "require": "./dist/node/index.cjs"
22
27
  }
23
28
  },
24
29
  "files": [
25
- "dist"
30
+ "dist",
31
+ "*.d.ts"
26
32
  ],
27
33
  "keywords": [
28
34
  "typescript",
@@ -46,16 +52,16 @@
46
52
  "devDependencies": {
47
53
  "@hypernym/eslint-config": "^1.0.1",
48
54
  "@hypernym/prettier-config": "^1.0.1",
49
- "@types/node": "^20.3.3",
55
+ "@types/node": "^20.4.1",
50
56
  "eslint": "^8.44.0",
51
57
  "prettier": "^2.8.8",
52
- "rollup": "^3.26.0",
58
+ "rollup": "^3.26.2",
53
59
  "rollup-plugin-dts": "^5.3.0",
54
60
  "rollup-plugin-esbuild": "^5.0.0",
55
61
  "typescript": "^5.0.4",
56
- "vite": "^4.3.9",
57
- "vite-node": "^0.32.2",
58
- "vitest": "^0.32.2"
62
+ "vite": "^4.4.3",
63
+ "vite-node": "^0.33.0",
64
+ "vitest": "^0.33.0"
59
65
  },
60
66
  "publishConfig": {
61
67
  "access": "public"