@ntnyq/utils 0.0.2 → 0.0.3

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,6 +14,8 @@ pnpm add @ntnyq/utils -D
14
14
  - [capitalize](./src/capitalize.ts)
15
15
  - [kekabCase](./src/kekabCase.ts)
16
16
 
17
+ - [waitFor](./src/waitFor.ts)
18
+
17
19
  ## License
18
20
 
19
21
  [MIT](./LICENSE) License © 2022-PRESENT [ntnyq](https://github.com/ntnyq)
package/dist/index.cjs CHANGED
@@ -12,6 +12,40 @@ function capitalize(input) {
12
12
  return `${input.charAt(0).toUpperCase()}${input.slice(1)}`;
13
13
  }
14
14
 
15
+ function waitFor(ms) {
16
+ return new Promise((resolve) => setTimeout(resolve, ms));
17
+ }
18
+
19
+ function toArray(val) {
20
+ val = val ?? [];
21
+ return Array.isArray(val) ? val : [val];
22
+ }
23
+
24
+ const isBrowser = () => typeof document !== "undefined";
25
+
26
+ const root = isBrowser() ? window : global;
27
+ let prev = Date.now();
28
+ function mockRAF(fn) {
29
+ const curr = Date.now();
30
+ const ms = Math.max(0, 16 - (curr - prev));
31
+ const id = setTimeout(fn, ms);
32
+ prev = curr + ms;
33
+ return id;
34
+ }
35
+ function rAF(fn) {
36
+ const raf = root.requestAnimationFrame || mockRAF;
37
+ return raf.call(root, fn);
38
+ }
39
+ function cAF(id) {
40
+ const caf = root.cancelAnimationFrame || root.clearTimeout;
41
+ return caf.call(root, id);
42
+ }
43
+
44
+ exports.cAF = cAF;
15
45
  exports.camelCase = camelCase;
16
46
  exports.capitalize = capitalize;
47
+ exports.isBrowser = isBrowser;
17
48
  exports.kekabCase = kekabCase;
49
+ exports.rAF = rAF;
50
+ exports.toArray = toArray;
51
+ exports.waitFor = waitFor;
@@ -0,0 +1,90 @@
1
+ import { Nullable, Arrayable } from '@ntnyq/types';
2
+
3
+ /**
4
+ * transform given string to kekabCase
5
+ *
6
+ * @param input given string
7
+ * @returns string in kekabCase
8
+ *
9
+ * @example
10
+ * ```
11
+ * import { kekabCase } from '@ntnyq/utils'
12
+ * kekabCase('FooBarBaz') // foo-bar-baz
13
+ * ```
14
+ */
15
+ declare function kekabCase(input: string): string;
16
+
17
+ /**
18
+ * transform given string to camelCase
19
+ *
20
+ * @param input given string
21
+ * @returns string in camelCase
22
+ *
23
+ * @example
24
+ * ```
25
+ * import { camelCase } from '@ntnyq/utils'
26
+ * camelCase('foo-bar-baz') // fooBarBaz
27
+ * ```
28
+ */
29
+ declare function camelCase(input: string): string;
30
+
31
+ /**
32
+ * capitalize the given string's first letter
33
+ *
34
+ * @param input given string
35
+ * @returns string first letter capitalize
36
+ *
37
+ * @example
38
+ * ```
39
+ * import { capitalize } from '@ntnyq/utils'
40
+ * capitalize('fooBarBaz') // FooBarBaz
41
+ * ```
42
+ */
43
+ declare function capitalize(input: string): string;
44
+
45
+ /**
46
+ * Wait for a number of milliseconds
47
+ *
48
+ * @param ms millseconds to wait
49
+ * @returns a promise that resolves after ms milliseconds
50
+ *
51
+ * @example
52
+ * ```
53
+ * import { waitFor } from '@ntnyq/utils'
54
+ * await waitFor(3e3)
55
+ * // do somthing after 3 seconds
56
+ * ```
57
+ */
58
+ declare function waitFor(ms: number): Promise<unknown>;
59
+
60
+ declare function toArray<T>(val?: Nullable<Arrayable<T>>): Arrayable<T>;
61
+
62
+ /**
63
+ * @file env.ts
64
+ */
65
+ /**
66
+ * Checks if the code is running in a browser
67
+ *
68
+ * @returns boolean - true if the code is running in a browser
69
+ */
70
+ declare const isBrowser: () => boolean;
71
+
72
+ /**
73
+ * @file raf.ts
74
+ */
75
+ /**
76
+ * Request animation frame
77
+ *
78
+ * @param fn callback
79
+ * @returns id
80
+ */
81
+ declare function rAF(fn: FrameRequestCallback): number;
82
+ /**
83
+ * Cancel animation frame
84
+ *
85
+ * @param id id
86
+ * @returns void
87
+ */
88
+ declare function cAF(id: number): void;
89
+
90
+ export { cAF, camelCase, capitalize, isBrowser, kekabCase, rAF, toArray, waitFor };
@@ -0,0 +1,90 @@
1
+ import { Nullable, Arrayable } from '@ntnyq/types';
2
+
3
+ /**
4
+ * transform given string to kekabCase
5
+ *
6
+ * @param input given string
7
+ * @returns string in kekabCase
8
+ *
9
+ * @example
10
+ * ```
11
+ * import { kekabCase } from '@ntnyq/utils'
12
+ * kekabCase('FooBarBaz') // foo-bar-baz
13
+ * ```
14
+ */
15
+ declare function kekabCase(input: string): string;
16
+
17
+ /**
18
+ * transform given string to camelCase
19
+ *
20
+ * @param input given string
21
+ * @returns string in camelCase
22
+ *
23
+ * @example
24
+ * ```
25
+ * import { camelCase } from '@ntnyq/utils'
26
+ * camelCase('foo-bar-baz') // fooBarBaz
27
+ * ```
28
+ */
29
+ declare function camelCase(input: string): string;
30
+
31
+ /**
32
+ * capitalize the given string's first letter
33
+ *
34
+ * @param input given string
35
+ * @returns string first letter capitalize
36
+ *
37
+ * @example
38
+ * ```
39
+ * import { capitalize } from '@ntnyq/utils'
40
+ * capitalize('fooBarBaz') // FooBarBaz
41
+ * ```
42
+ */
43
+ declare function capitalize(input: string): string;
44
+
45
+ /**
46
+ * Wait for a number of milliseconds
47
+ *
48
+ * @param ms millseconds to wait
49
+ * @returns a promise that resolves after ms milliseconds
50
+ *
51
+ * @example
52
+ * ```
53
+ * import { waitFor } from '@ntnyq/utils'
54
+ * await waitFor(3e3)
55
+ * // do somthing after 3 seconds
56
+ * ```
57
+ */
58
+ declare function waitFor(ms: number): Promise<unknown>;
59
+
60
+ declare function toArray<T>(val?: Nullable<Arrayable<T>>): Arrayable<T>;
61
+
62
+ /**
63
+ * @file env.ts
64
+ */
65
+ /**
66
+ * Checks if the code is running in a browser
67
+ *
68
+ * @returns boolean - true if the code is running in a browser
69
+ */
70
+ declare const isBrowser: () => boolean;
71
+
72
+ /**
73
+ * @file raf.ts
74
+ */
75
+ /**
76
+ * Request animation frame
77
+ *
78
+ * @param fn callback
79
+ * @returns id
80
+ */
81
+ declare function rAF(fn: FrameRequestCallback): number;
82
+ /**
83
+ * Cancel animation frame
84
+ *
85
+ * @param id id
86
+ * @returns void
87
+ */
88
+ declare function cAF(id: number): void;
89
+
90
+ export { cAF, camelCase, capitalize, isBrowser, kekabCase, rAF, toArray, waitFor };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { Nullable, Arrayable } from '@ntnyq/types';
2
+
1
3
  /**
2
4
  * transform given string to kekabCase
3
5
  *
@@ -40,4 +42,49 @@ declare function camelCase(input: string): string;
40
42
  */
41
43
  declare function capitalize(input: string): string;
42
44
 
43
- export { camelCase, capitalize, kekabCase };
45
+ /**
46
+ * Wait for a number of milliseconds
47
+ *
48
+ * @param ms millseconds to wait
49
+ * @returns a promise that resolves after ms milliseconds
50
+ *
51
+ * @example
52
+ * ```
53
+ * import { waitFor } from '@ntnyq/utils'
54
+ * await waitFor(3e3)
55
+ * // do somthing after 3 seconds
56
+ * ```
57
+ */
58
+ declare function waitFor(ms: number): Promise<unknown>;
59
+
60
+ declare function toArray<T>(val?: Nullable<Arrayable<T>>): Arrayable<T>;
61
+
62
+ /**
63
+ * @file env.ts
64
+ */
65
+ /**
66
+ * Checks if the code is running in a browser
67
+ *
68
+ * @returns boolean - true if the code is running in a browser
69
+ */
70
+ declare const isBrowser: () => boolean;
71
+
72
+ /**
73
+ * @file raf.ts
74
+ */
75
+ /**
76
+ * Request animation frame
77
+ *
78
+ * @param fn callback
79
+ * @returns id
80
+ */
81
+ declare function rAF(fn: FrameRequestCallback): number;
82
+ /**
83
+ * Cancel animation frame
84
+ *
85
+ * @param id id
86
+ * @returns void
87
+ */
88
+ declare function cAF(id: number): void;
89
+
90
+ export { cAF, camelCase, capitalize, isBrowser, kekabCase, rAF, toArray, waitFor };
package/dist/index.mjs CHANGED
@@ -10,4 +10,33 @@ function capitalize(input) {
10
10
  return `${input.charAt(0).toUpperCase()}${input.slice(1)}`;
11
11
  }
12
12
 
13
- export { camelCase, capitalize, kekabCase };
13
+ function waitFor(ms) {
14
+ return new Promise((resolve) => setTimeout(resolve, ms));
15
+ }
16
+
17
+ function toArray(val) {
18
+ val = val ?? [];
19
+ return Array.isArray(val) ? val : [val];
20
+ }
21
+
22
+ const isBrowser = () => typeof document !== "undefined";
23
+
24
+ const root = isBrowser() ? window : global;
25
+ let prev = Date.now();
26
+ function mockRAF(fn) {
27
+ const curr = Date.now();
28
+ const ms = Math.max(0, 16 - (curr - prev));
29
+ const id = setTimeout(fn, ms);
30
+ prev = curr + ms;
31
+ return id;
32
+ }
33
+ function rAF(fn) {
34
+ const raf = root.requestAnimationFrame || mockRAF;
35
+ return raf.call(root, fn);
36
+ }
37
+ function cAF(id) {
38
+ const caf = root.cancelAnimationFrame || root.clearTimeout;
39
+ return caf.call(root, id);
40
+ }
41
+
42
+ export { cAF, camelCase, capitalize, isBrowser, kekabCase, rAF, toArray, waitFor };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ntnyq/utils",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -18,12 +18,17 @@
18
18
  "directory": "packages/utils"
19
19
  },
20
20
  "exports": {
21
+ "./package.json": "./package.json",
21
22
  ".": {
22
- "types": "./dist/index.d.ts",
23
- "require": "./dist/index.cjs",
24
- "import": "./dist/index.mjs"
25
- },
26
- "./*": "./*"
23
+ "import": {
24
+ "types": "./dist/index.d.mts",
25
+ "default": "./dist/index.mjs"
26
+ },
27
+ "require": {
28
+ "types": "./dist/index.d.cts",
29
+ "default": "./dist/index.cjs"
30
+ }
31
+ }
27
32
  },
28
33
  "main": "./dist/index.cjs",
29
34
  "module": "./dist/index.mjs",
@@ -43,6 +48,9 @@
43
48
  "bugs": {
44
49
  "url": "https://github.com/ntnyq/ntnyq-utils/issues"
45
50
  },
51
+ "dependencies": {
52
+ "@ntnyq/types": "0.0.2"
53
+ },
46
54
  "scripts": {
47
55
  "build": "unbuild",
48
56
  "dev": "unbuild --stub"