@hypernym/utils 3.1.0 → 3.2.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/README.md +168 -1
- package/dist/fs/index.mjs +27 -7
- package/dist/index.iife.js +1 -0
- package/dist/index.min.mjs +1 -0
- package/dist/index.umd.js +1 -0
- package/dist/types/fs/index.d.ts +94 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,7 +14,9 @@
|
|
|
14
14
|
|
|
15
15
|
<pre align="center">pnpm add @hypernym/utils</pre>
|
|
16
16
|
|
|
17
|
-
<
|
|
17
|
+
<p align="center">
|
|
18
|
+
<strong>Hypernym Studio</strong>
|
|
19
|
+
</p>
|
|
18
20
|
|
|
19
21
|
<br>
|
|
20
22
|
|
|
@@ -26,14 +28,179 @@
|
|
|
26
28
|
|
|
27
29
|
## Usage
|
|
28
30
|
|
|
31
|
+
After installation, import `Hyperutils` into your project:
|
|
32
|
+
|
|
29
33
|
```ts
|
|
30
34
|
// ESM & TS
|
|
31
35
|
import { isNull, isString, ... } from '@hypernym/utils'
|
|
32
36
|
|
|
37
|
+
// ESM & TS
|
|
38
|
+
import { exists, copy, ... } from '@hypernym/utils/fs'
|
|
39
|
+
|
|
33
40
|
// Types
|
|
34
41
|
import type { IsAny, RequiredDeep, ... } from '@hypernym/utils'
|
|
35
42
|
```
|
|
36
43
|
|
|
44
|
+
## CDN
|
|
45
|
+
|
|
46
|
+
Here are some examples of how to integrate **Hyperutils** from a CDN via a script tag.
|
|
47
|
+
|
|
48
|
+
Also, it is possible to download files manually and serve them accordingly.
|
|
49
|
+
|
|
50
|
+
#### minified esm
|
|
51
|
+
|
|
52
|
+
```html
|
|
53
|
+
<script type="module">
|
|
54
|
+
import { isNull, isString, ... } from 'https://unpkg.com/@hypernym/utils/dist/index.min.mjs'
|
|
55
|
+
</script>
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
#### minified iief
|
|
59
|
+
|
|
60
|
+
```html
|
|
61
|
+
<script src="https://unpkg.com/@hypernym/utils/dist/index.iief.mjs"></script>
|
|
62
|
+
<script>
|
|
63
|
+
const { isNull, isString, ... } = Hyperutils
|
|
64
|
+
</script>
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
#### minified umd
|
|
68
|
+
|
|
69
|
+
```html
|
|
70
|
+
<script src="https://unpkg.com/@hypernym/utils/dist/index.umd.mjs"></script>
|
|
71
|
+
<script>
|
|
72
|
+
const { isNull, isString, ... } = Hyperutils
|
|
73
|
+
</script>
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## is
|
|
77
|
+
|
|
78
|
+
### isBrowser
|
|
79
|
+
|
|
80
|
+
Checks if the code is running in the browser.
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { isBrowser } from '@hypernym/utils'
|
|
84
|
+
|
|
85
|
+
isBrowser // true
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### isNull
|
|
89
|
+
|
|
90
|
+
Returns a boolean if the given value is a `null`.
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
import { isNull } from '@hypernym/utils'
|
|
94
|
+
|
|
95
|
+
isNull(null) // => true
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### isUndefined
|
|
99
|
+
|
|
100
|
+
Returns a boolean if the given value is a `undefined`.
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
import { isUndefined } from '@hypernym/utils'
|
|
104
|
+
|
|
105
|
+
isUndefined(undefined) // => true
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### isString
|
|
109
|
+
|
|
110
|
+
Returns a boolean if the given value is a `string`.
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
import { isString } from '@hypernym/utils'
|
|
114
|
+
|
|
115
|
+
isString('@hypernym/utils') // => true
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## fs
|
|
119
|
+
|
|
120
|
+
### exists
|
|
121
|
+
|
|
122
|
+
Checks if the file or directory exists.
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
import { exists } from '@hypernym/utils/fs'
|
|
126
|
+
|
|
127
|
+
await exists('dir/file.ts') // => true
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### write
|
|
131
|
+
|
|
132
|
+
Writes data to a file recursively.
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
import { write } from '@hypernym/utils/fs'
|
|
136
|
+
|
|
137
|
+
await write('dir/subdir/file.ts', `console.log('Hello World!')`)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### copy
|
|
141
|
+
|
|
142
|
+
Copies `files` or `directories` recursively.
|
|
143
|
+
|
|
144
|
+
Accepts a single source or a range of sources.
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
import { copy } from '@hypernym/utils/fs'
|
|
148
|
+
|
|
149
|
+
await copy('src/subdir/file.ts', './dist/subdir')
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### mkdir
|
|
153
|
+
|
|
154
|
+
Creates a `directory` recursively.
|
|
155
|
+
|
|
156
|
+
Accepts a single path or a range of paths.
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
import { mkdir } from '@hypernym/utils/fs'
|
|
160
|
+
|
|
161
|
+
await mkdir('src/subdir')
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### remove
|
|
165
|
+
|
|
166
|
+
Removes `files` and `directories` recursively.
|
|
167
|
+
|
|
168
|
+
Accepts a single path or a range of paths.
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
import { remove } from '@hypernym/utils/fs'
|
|
172
|
+
|
|
173
|
+
await remove('src/subdir/file.ts')
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Types
|
|
177
|
+
|
|
178
|
+
### PartialDeep
|
|
179
|
+
|
|
180
|
+
Constructs a type by recursively setting all properties as optional.
|
|
181
|
+
|
|
182
|
+
Use `Partial<T>` for one level.
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
type PartialObject = PartialDeep<Object>
|
|
186
|
+
|
|
187
|
+
// Disables recursive mode for arrays and tuples.
|
|
188
|
+
type PartialObject = PartialDeep<Object, { arrays: false }>
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### RequiredDeep
|
|
192
|
+
|
|
193
|
+
Constructs a type by recursively setting all properties as required.
|
|
194
|
+
|
|
195
|
+
Use `Required<T>` for one level.
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
type RequiredObject = RequiredDeep<Object>
|
|
199
|
+
|
|
200
|
+
// Disables recursive mode for arrays and tuples.
|
|
201
|
+
type RequiredObject = RequiredDeep<Object, { arrays: false }>
|
|
202
|
+
```
|
|
203
|
+
|
|
37
204
|
## Community
|
|
38
205
|
|
|
39
206
|
Feel free to ask questions or share new ideas.
|
package/dist/fs/index.mjs
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
import { access, constants, mkdir, writeFile as writeFile$1, cp } from 'node:fs/promises';
|
|
1
|
+
import { access, constants, mkdir as mkdir$1, writeFile as writeFile$1, cp, rm } from 'node:fs/promises';
|
|
2
2
|
import { dirname } from 'node:path';
|
|
3
|
-
import {
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { isURL, isString } from '../index.mjs';
|
|
4
5
|
|
|
5
6
|
async function exists(path) {
|
|
6
7
|
return await access(path, constants.F_OK).then(() => true).catch(() => false);
|
|
7
8
|
}
|
|
8
9
|
|
|
9
|
-
async function
|
|
10
|
-
await mkdir(dirname(path)
|
|
11
|
-
|
|
10
|
+
async function write(path, data, options) {
|
|
11
|
+
await mkdir$1(dirname(isURL(path) ? fileURLToPath(path) : path), {
|
|
12
|
+
recursive: true
|
|
13
|
+
});
|
|
14
|
+
await writeFile$1(path, data, options);
|
|
12
15
|
}
|
|
16
|
+
const writeFile = write;
|
|
13
17
|
|
|
14
|
-
async function copy(source, destination, options) {
|
|
18
|
+
async function copy(source, destination, options = {}) {
|
|
15
19
|
const { recursive = true, filter } = options;
|
|
16
20
|
const sources = isString(source) || isURL(source) ? [source] : source;
|
|
17
21
|
for (const src of sources) {
|
|
@@ -22,4 +26,20 @@ async function copy(source, destination, options) {
|
|
|
22
26
|
}
|
|
23
27
|
}
|
|
24
28
|
|
|
25
|
-
|
|
29
|
+
async function mkdir(path, options = {}) {
|
|
30
|
+
const { recursive = true, mode } = options;
|
|
31
|
+
const paths = isString(path) || isURL(path) ? [path] : path;
|
|
32
|
+
for (const p of paths) {
|
|
33
|
+
await mkdir$1(p, { recursive, mode });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function remove(path, options = {}) {
|
|
38
|
+
const { recursive = true, force = true } = options;
|
|
39
|
+
const paths = isString(path) || isURL(path) ? [path] : path;
|
|
40
|
+
for (const p of paths) {
|
|
41
|
+
await rm(p, { recursive, force, ...options });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { copy, exists, mkdir, remove, write, writeFile };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var Hyperutils=function(t){"use strict";const u=()=>{},e=i=>Object.prototype.toString.call(i).slice(8,-1),N=typeof window<"u",s=i=>i===null,l=i=>typeof i>"u",n=i=>typeof i=="string",S=i=>n(i)&&i.trim().length===0,a=i=>typeof i=="boolean",b=i=>i===!0,r=i=>i===!1,c=i=>typeof i=="number"&&!isNaN(i),m=i=>Array.isArray(i),d=i=>m(i)&&i.length===0,y=i=>e(i)==="Object",L=i=>y(i)&&Object.keys(i).length===0,H=i=>i instanceof Function,M=i=>typeof i=="number"&&isNaN(i),O=i=>i instanceof RegExp,j=i=>i instanceof Map,k=i=>i instanceof WeakMap,v=i=>i instanceof Set,A=i=>i instanceof WeakSet,o=i=>e(i)==="Symbol",B=i=>i instanceof Date&&!isNaN(i.valueOf()),f=i=>typeof i=="bigint",R=i=>i===1/0||i===-1/0,W=i=>i instanceof URL,h=i=>i instanceof Error,C=i=>n(i)||c(i)||f(i)||a(i)||o(i)||s(i)||l(i),F=i=>i instanceof Element,U=i=>i instanceof HTMLElement,w=i=>i instanceof SVGElement,E=i=>i instanceof NodeList,I=i=>E(i)&&i.length===0,g=i=>i instanceof HTMLCollection,T=i=>g(i)&&i.length===0;return t.isArray=m,t.isArrayEmpty=d,t.isBigInt=f,t.isBoolean=a,t.isBrowser=N,t.isDate=B,t.isElement=F,t.isError=h,t.isFalse=r,t.isFunction=H,t.isHtmlCollection=g,t.isHtmlCollectionEmpty=T,t.isHtmlElement=U,t.isInfinity=R,t.isMap=j,t.isNaNValue=M,t.isNodeList=E,t.isNodeListEmpty=I,t.isNull=s,t.isNumber=c,t.isObject=y,t.isObjectEmpty=L,t.isPrimitive=C,t.isRegExp=O,t.isSet=v,t.isString=n,t.isStringEmpty=S,t.isSvgElement=w,t.isSymbol=o,t.isTrue=b,t.isURL=W,t.isUndefined=l,t.isWeakMap=k,t.isWeakSet=A,t.noop=u,t.toString=e,t}({});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const y=()=>{},i=e=>Object.prototype.toString.call(e).slice(8,-1),g=typeof window<"u",s=e=>e===null,n=e=>typeof e>"u",t=e=>typeof e=="string",E=e=>t(e)&&e.trim().length===0,o=e=>typeof e=="boolean",N=e=>e===!0,b=e=>e===!1,a=e=>typeof e=="number"&&!isNaN(e),l=e=>Array.isArray(e),u=e=>l(e)&&e.length===0,r=e=>i(e)==="Object",S=e=>r(e)&&Object.keys(e).length===0,L=e=>e instanceof Function,d=e=>typeof e=="number"&&isNaN(e),M=e=>e instanceof RegExp,O=e=>e instanceof Map,h=e=>e instanceof WeakMap,j=e=>e instanceof Set,k=e=>e instanceof WeakSet,c=e=>i(e)==="Symbol",H=e=>e instanceof Date&&!isNaN(e.valueOf()),f=e=>typeof e=="bigint",A=e=>e===1/0||e===-1/0,R=e=>e instanceof URL,W=e=>e instanceof Error,v=e=>t(e)||a(e)||f(e)||o(e)||c(e)||s(e)||n(e),w=e=>e instanceof Element,x=e=>e instanceof HTMLElement,B=e=>e instanceof SVGElement,p=e=>e instanceof NodeList,C=e=>p(e)&&e.length===0,m=e=>e instanceof HTMLCollection,F=e=>m(e)&&e.length===0;export{l as isArray,u as isArrayEmpty,f as isBigInt,o as isBoolean,g as isBrowser,H as isDate,w as isElement,W as isError,b as isFalse,L as isFunction,m as isHtmlCollection,F as isHtmlCollectionEmpty,x as isHtmlElement,A as isInfinity,O as isMap,d as isNaNValue,p as isNodeList,C as isNodeListEmpty,s as isNull,a as isNumber,r as isObject,S as isObjectEmpty,v as isPrimitive,M as isRegExp,j as isSet,t as isString,E as isStringEmpty,B as isSvgElement,c as isSymbol,N as isTrue,R as isURL,n as isUndefined,h as isWeakMap,k as isWeakSet,y as noop,i as toString};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(e,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(e=typeof globalThis<"u"?globalThis:e||self,n(e.Hyperutils={}))})(this,function(e){"use strict";const n=()=>{},t=i=>Object.prototype.toString.call(i).slice(8,-1),d=typeof window<"u",l=i=>i===null,a=i=>typeof i>"u",s=i=>typeof i=="string",N=i=>s(i)&&i.trim().length===0,o=i=>typeof i=="boolean",S=i=>i===!0,b=i=>i===!1,f=i=>typeof i=="number"&&!isNaN(i),m=i=>Array.isArray(i),r=i=>m(i)&&i.length===0,c=i=>t(i)==="Object",L=i=>c(i)&&Object.keys(i).length===0,H=i=>i instanceof Function,h=i=>typeof i=="number"&&isNaN(i),j=i=>i instanceof RegExp,M=i=>i instanceof Map,O=i=>i instanceof WeakMap,k=i=>i instanceof Set,A=i=>i instanceof WeakSet,y=i=>t(i)==="Symbol",B=i=>i instanceof Date&&!isNaN(i.valueOf()),u=i=>typeof i=="bigint",R=i=>i===1/0||i===-1/0,T=i=>i instanceof URL,W=i=>i instanceof Error,v=i=>s(i)||f(i)||u(i)||o(i)||y(i)||l(i)||a(i),C=i=>i instanceof Element,F=i=>i instanceof HTMLElement,U=i=>i instanceof SVGElement,E=i=>i instanceof NodeList,w=i=>E(i)&&i.length===0,g=i=>i instanceof HTMLCollection,I=i=>g(i)&&i.length===0;e.isArray=m,e.isArrayEmpty=r,e.isBigInt=u,e.isBoolean=o,e.isBrowser=d,e.isDate=B,e.isElement=C,e.isError=W,e.isFalse=b,e.isFunction=H,e.isHtmlCollection=g,e.isHtmlCollectionEmpty=I,e.isHtmlElement=F,e.isInfinity=R,e.isMap=M,e.isNaNValue=h,e.isNodeList=E,e.isNodeListEmpty=w,e.isNull=l,e.isNumber=f,e.isObject=c,e.isObjectEmpty=L,e.isPrimitive=v,e.isRegExp=j,e.isSet=k,e.isString=s,e.isStringEmpty=N,e.isSvgElement=U,e.isSymbol=y,e.isTrue=S,e.isURL=T,e.isUndefined=a,e.isWeakMap=O,e.isWeakSet=A,e.noop=n,e.toString=t});
|
package/dist/types/fs/index.d.ts
CHANGED
|
@@ -13,19 +13,26 @@ import { writeFile as writeFile$1 } from 'node:fs/promises';
|
|
|
13
13
|
*/
|
|
14
14
|
declare function exists(path: string): Promise<boolean>;
|
|
15
15
|
|
|
16
|
+
type WritePath = string | URL;
|
|
16
17
|
/**
|
|
17
18
|
* Writes data to a file recursively.
|
|
18
19
|
*
|
|
19
20
|
* @example
|
|
20
21
|
*
|
|
21
22
|
* ```ts
|
|
22
|
-
* import {
|
|
23
|
+
* import { write } from '@hypernym/utils/fs'
|
|
23
24
|
*
|
|
24
|
-
* await
|
|
25
|
+
* await write('dir/subdir/file.ts', `console.log('Hello World!')`)
|
|
25
26
|
* ```
|
|
26
27
|
*/
|
|
27
|
-
declare function
|
|
28
|
+
declare function write(path: WritePath, data: Parameters<typeof writeFile$1>[1], options?: Parameters<typeof writeFile$1>[2]): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* @deprecated Use `write` instead.
|
|
31
|
+
*/
|
|
32
|
+
declare const writeFile: typeof write;
|
|
28
33
|
|
|
34
|
+
type CopySource = string | URL;
|
|
35
|
+
type CopyDestination = string | URL;
|
|
29
36
|
interface CopyOptions {
|
|
30
37
|
/**
|
|
31
38
|
* Copies files or directories recursively.
|
|
@@ -55,6 +62,88 @@ interface CopyOptions {
|
|
|
55
62
|
* await copy('src/subdir/file.ts', './dist/subdir')
|
|
56
63
|
* ```
|
|
57
64
|
*/
|
|
58
|
-
declare function copy(source:
|
|
65
|
+
declare function copy(source: CopySource | CopySource[], destination: CopyDestination, options?: CopyOptions): Promise<void>;
|
|
66
|
+
|
|
67
|
+
type MakeDirPath = string | URL;
|
|
68
|
+
interface MakeDirOptions {
|
|
69
|
+
/**
|
|
70
|
+
* Indicates whether parent folders should be created.
|
|
71
|
+
*
|
|
72
|
+
* If a folder was created, the path to the first created folder will be returned.
|
|
73
|
+
*
|
|
74
|
+
* @default true
|
|
75
|
+
*/
|
|
76
|
+
recursive?: boolean;
|
|
77
|
+
/**
|
|
78
|
+
* A file mode. If a string is passed, it is parsed as an octal integer.
|
|
79
|
+
*
|
|
80
|
+
* @default 0o777
|
|
81
|
+
*/
|
|
82
|
+
mode?: string | number;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Creates a `directory` recursively.
|
|
86
|
+
*
|
|
87
|
+
* Accepts a single path or a range of paths.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
*
|
|
91
|
+
* ```ts
|
|
92
|
+
* import { mkdir } from '@hypernym/utils/fs'
|
|
93
|
+
*
|
|
94
|
+
* await mkdir('src/subdir')
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
declare function mkdir(path: MakeDirPath | MakeDirPath[], options?: MakeDirOptions): Promise<void>;
|
|
98
|
+
|
|
99
|
+
type RemovePath = string | URL;
|
|
100
|
+
interface RemoveOptions {
|
|
101
|
+
/**
|
|
102
|
+
* If `true`, perform a recursive directory removal.
|
|
103
|
+
*
|
|
104
|
+
* In recursive mode, operations are retried on failure.
|
|
105
|
+
*
|
|
106
|
+
* @default true
|
|
107
|
+
*/
|
|
108
|
+
recursive?: boolean;
|
|
109
|
+
/**
|
|
110
|
+
* When `true`, exceptions will be ignored if `path` does not exist.
|
|
111
|
+
*
|
|
112
|
+
* @default true
|
|
113
|
+
*/
|
|
114
|
+
force?: boolean;
|
|
115
|
+
/**
|
|
116
|
+
* If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or
|
|
117
|
+
* `EPERM` error is encountered, Node.js will retry the operation with a linear
|
|
118
|
+
* backoff wait of `retryDelay` ms longer on each try. This option represents the
|
|
119
|
+
* number of retries. This option is ignored if the `recursive` option is not
|
|
120
|
+
* `true`.
|
|
121
|
+
*
|
|
122
|
+
* @default 0
|
|
123
|
+
*/
|
|
124
|
+
maxRetries?: number;
|
|
125
|
+
/**
|
|
126
|
+
* The amount of time in milliseconds to wait between retries.
|
|
127
|
+
*
|
|
128
|
+
* This option is ignored if the `recursive` option is not `true`.
|
|
129
|
+
*
|
|
130
|
+
* @default 100
|
|
131
|
+
*/
|
|
132
|
+
retryDelay?: number;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Removes `files` and `directories` recursively.
|
|
136
|
+
*
|
|
137
|
+
* Accepts a single path or a range of paths.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
*
|
|
141
|
+
* ```ts
|
|
142
|
+
* import { remove } from '@hypernym/utils/fs'
|
|
143
|
+
*
|
|
144
|
+
* await remove('src/subdir/file.ts')
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
declare function remove(path: RemovePath | RemovePath[], options?: RemoveOptions): Promise<void>;
|
|
59
148
|
|
|
60
|
-
export { type CopyOptions, copy, exists, writeFile };
|
|
149
|
+
export { type CopyDestination, type CopyOptions, type CopySource, type MakeDirOptions, type MakeDirPath, type RemoveOptions, type RemovePath, type WritePath, copy, exists, mkdir, remove, write, writeFile };
|