@hypernym/utils 3.2.0 → 3.3.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/README.md CHANGED
@@ -14,7 +14,9 @@
14
14
 
15
15
  <pre align="center">pnpm add @hypernym/utils</pre>
16
16
 
17
- <h4 align="center">Hypernym Studio</h4>
17
+ <p align="center">
18
+ <strong>Hypernym Studio</strong>
19
+ </p>
18
20
 
19
21
  <br>
20
22
 
@@ -26,14 +28,189 @@
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
+ ### read
131
+
132
+ Reads the entire contents of a file.
133
+
134
+ ```ts
135
+ import { read } from '@hypernym/utils/fs'
136
+
137
+ await read('dir/subdir/file.ts')
138
+ ```
139
+
140
+ ### write
141
+
142
+ Writes data to a file recursively.
143
+
144
+ ```ts
145
+ import { write } from '@hypernym/utils/fs'
146
+
147
+ await write('dir/subdir/file.ts', `console.log('Hello World!')`)
148
+ ```
149
+
150
+ ### copy
151
+
152
+ Copies `files` or `directories` recursively.
153
+
154
+ Accepts a single source or a range of sources.
155
+
156
+ ```ts
157
+ import { copy } from '@hypernym/utils/fs'
158
+
159
+ await copy('src/subdir/file.ts', './dist/subdir')
160
+ ```
161
+
162
+ ### mkdir
163
+
164
+ Creates a `directory` recursively.
165
+
166
+ Accepts a single path or a range of paths.
167
+
168
+ ```ts
169
+ import { mkdir } from '@hypernym/utils/fs'
170
+
171
+ await mkdir('src/subdir')
172
+ ```
173
+
174
+ ### remove
175
+
176
+ Removes `files` and `directories` recursively.
177
+
178
+ Accepts a single path or a range of paths.
179
+
180
+ ```ts
181
+ import { remove } from '@hypernym/utils/fs'
182
+
183
+ await remove('src/subdir/file.ts')
184
+ ```
185
+
186
+ ## Types
187
+
188
+ ### PartialDeep
189
+
190
+ Constructs a type by recursively setting all properties as optional.
191
+
192
+ Use `Partial<T>` for one level.
193
+
194
+ ```ts
195
+ type PartialObject = PartialDeep<Object>
196
+
197
+ // Disables recursive mode for arrays and tuples.
198
+ type PartialObject = PartialDeep<Object, { arrays: false }>
199
+ ```
200
+
201
+ ### RequiredDeep
202
+
203
+ Constructs a type by recursively setting all properties as required.
204
+
205
+ Use `Required<T>` for one level.
206
+
207
+ ```ts
208
+ type RequiredObject = RequiredDeep<Object>
209
+
210
+ // Disables recursive mode for arrays and tuples.
211
+ type RequiredObject = RequiredDeep<Object, { arrays: false }>
212
+ ```
213
+
37
214
  ## Community
38
215
 
39
216
  Feel free to ask questions or share new ideas.
package/dist/fs/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { access, constants, mkdir as mkdir$1, writeFile as writeFile$1, cp, rm } from 'node:fs/promises';
1
+ import { access, constants, readFile, mkdir as mkdir$1, writeFile as writeFile$1, cp, rm } from 'node:fs/promises';
2
2
  import { dirname } from 'node:path';
3
3
  import { fileURLToPath } from 'node:url';
4
4
  import { isURL, isString } from '../index.mjs';
@@ -7,6 +7,11 @@ async function exists(path) {
7
7
  return await access(path, constants.F_OK).then(() => true).catch(() => false);
8
8
  }
9
9
 
10
+ async function read(path, options = {}) {
11
+ const { encoding = "utf-8" } = options;
12
+ return await readFile(path, { encoding, ...options });
13
+ }
14
+
10
15
  async function write(path, data, options) {
11
16
  await mkdir$1(dirname(isURL(path) ? fileURLToPath(path) : path), {
12
17
  recursive: true
@@ -42,4 +47,4 @@ async function remove(path, options = {}) {
42
47
  }
43
48
  }
44
49
 
45
- export { copy, exists, mkdir, remove, write, writeFile };
50
+ export { copy, exists, mkdir, read, 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});
@@ -13,6 +13,42 @@ import { writeFile as writeFile$1 } from 'node:fs/promises';
13
13
  */
14
14
  declare function exists(path: string): Promise<boolean>;
15
15
 
16
+ type ReadPath = string | URL;
17
+ type ReadEncodingType = BufferEncoding | null | undefined;
18
+ type ReadType<T> = T extends BufferEncoding | undefined ? string : T extends null ? Buffer : never;
19
+ interface ReadOptions<T extends ReadEncodingType> {
20
+ /**
21
+ * If no encoding is specified, the data is returned as a `Buffer` object. Otherwise, the data will be a string.
22
+ *
23
+ * @default 'utf-8'
24
+ */
25
+ encoding?: T;
26
+ /**
27
+ * When provided the corresponding `AbortController` can be used to cancel an asynchronous action.
28
+ *
29
+ * @default undefined
30
+ */
31
+ signal?: AbortSignal;
32
+ /**
33
+ * If a flag is not provided, it defaults to `'r'`.
34
+ *
35
+ * @default 'r'
36
+ */
37
+ flag?: number | string;
38
+ }
39
+ /**
40
+ * Reads the entire contents of a file.
41
+ *
42
+ * @example
43
+ *
44
+ * ```ts
45
+ * import { read } from '@hypernym/utils/fs'
46
+ *
47
+ * await read('dir/subdir/file.ts')
48
+ * ```
49
+ */
50
+ declare function read<T extends ReadEncodingType = BufferEncoding>(path: ReadPath, options?: ReadOptions<T>): Promise<ReadType<T>>;
51
+
16
52
  type WritePath = string | URL;
17
53
  /**
18
54
  * Writes data to a file recursively.
@@ -146,4 +182,4 @@ interface RemoveOptions {
146
182
  */
147
183
  declare function remove(path: RemovePath | RemovePath[], options?: RemoveOptions): Promise<void>;
148
184
 
149
- export { type CopyDestination, type CopyOptions, type CopySource, type MakeDirOptions, type MakeDirPath, type RemoveOptions, type RemovePath, type WritePath, copy, exists, mkdir, remove, write, writeFile };
185
+ export { type CopyDestination, type CopyOptions, type CopySource, type MakeDirOptions, type MakeDirPath, type ReadEncodingType, type ReadOptions, type ReadPath, type ReadType, type RemoveOptions, type RemovePath, type WritePath, copy, exists, mkdir, read, remove, write, writeFile };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hypernym/utils",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "author": "Hypernym Studio",
5
5
  "description": "A collection of reusable utilities.",
6
6
  "license": "MIT",
@@ -66,7 +66,7 @@
66
66
  "@hypernym/bundler": "^0.11.0",
67
67
  "@hypernym/eslint-config": "^3.5.0",
68
68
  "@hypernym/prettier-config": "^3.2.0",
69
- "@hypernym/tsconfig": "^2.3.0",
69
+ "@hypernym/tsconfig": "^2.4.0",
70
70
  "@types/node": "^22.5.5",
71
71
  "eslint": "^9.10.0",
72
72
  "prettier": "^3.3.3",