@ntnyq/utils 0.0.3 → 0.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.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022-PRESENT ntnyq <https://github.com/ntnyq>
3
+ Copyright (c) 2024-PRESENT ntnyq <https://github.com/ntnyq>
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,21 +1,27 @@
1
1
  # @ntnyq/utils
2
2
 
3
+ [![CI](https://github.com/ntnyq/utils/workflows/CI/badge.svg)](https://github.com/ntnyq/utils/actions)
3
4
  [![NPM VERSION](https://img.shields.io/npm/v/@ntnyq/utils.svg)](https://www.npmjs.com/package/@ntnyq/utils)
5
+ [![NPM DOWNLOADS](https://img.shields.io/npm/dy/@ntnyq/utils.svg)](https://www.npmjs.com/package/@ntnyq/utils)
6
+ [![CODECOV](https://codecov.io/github/ntnyq/utils/branch/main/graph/badge.svg)](https://codecov.io/github/ntnyq/utils)
7
+ [![LICENSE](https://img.shields.io/github/license/ntnyq/utils.svg)](https://github.com/ntnyq/utils/blob/main/LICENSE)
8
+
9
+ > Common used utils.
4
10
 
5
11
  ## Install
6
12
 
7
13
  ```bash
8
- pnpm add @ntnyq/utils -D
14
+ npm install @ntnyq/utils
9
15
  ```
10
16
 
11
- ## Utils
12
-
13
- - [camelCase](./src/camelCase.ts)
14
- - [capitalize](./src/capitalize.ts)
15
- - [kekabCase](./src/kekabCase.ts)
17
+ ```bash
18
+ yarn add @ntnyq/utils
19
+ ```
16
20
 
17
- - [waitFor](./src/waitFor.ts)
21
+ ```bash
22
+ pnpm add @ntnyq/utils
23
+ ```
18
24
 
19
25
  ## License
20
26
 
21
- [MIT](./LICENSE) License © 2022-PRESENT [ntnyq](https://github.com/ntnyq)
27
+ [MIT](./LICENSE) License © 2024-PRESENT [ntnyq](https://github.com/ntnyq)
package/dist/index.cjs CHANGED
@@ -1,30 +1,150 @@
1
- 'use strict';
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
2
19
 
3
- function kekabCase(input) {
4
- return input.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase();
5
- }
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ NOOP: () => NOOP,
24
+ cAF: () => cAF,
25
+ camelCase: () => camelCase,
26
+ capitalize: () => capitalize,
27
+ days: () => days,
28
+ flatCase: () => flatCase,
29
+ hours: () => hours,
30
+ isBrowser: () => isBrowser,
31
+ isUppercase: () => isUppercase,
32
+ join: () => join,
33
+ kebabCase: () => kebabCase,
34
+ lowerFirst: () => lowerFirst,
35
+ minutes: () => minutes,
36
+ noop: () => noop,
37
+ pascalCase: () => pascalCase,
38
+ rAF: () => rAF,
39
+ seconds: () => seconds,
40
+ snakeCase: () => snakeCase,
41
+ splitByCase: () => splitByCase,
42
+ titleCase: () => titleCase,
43
+ toArray: () => toArray,
44
+ trainCase: () => trainCase,
45
+ unique: () => unique,
46
+ uniqueBy: () => uniqueBy,
47
+ upperFirst: () => upperFirst,
48
+ waitFor: () => waitFor,
49
+ warnOnce: () => warnOnce,
50
+ weeks: () => weeks
51
+ });
52
+ module.exports = __toCommonJS(src_exports);
6
53
 
7
- function camelCase(input) {
8
- return input.replace(/[._-](\w)/g, (_, c) => c ? c.toUpperCase() : "");
9
- }
54
+ // src/fn/noop.ts
55
+ var noop = () => {
56
+ };
57
+ var NOOP = noop;
10
58
 
11
- function capitalize(input) {
12
- return `${input.charAt(0).toUpperCase()}${input.slice(1)}`;
13
- }
59
+ // src/env/isBrowser.ts
60
+ var isBrowser = () => typeof document !== "undefined";
14
61
 
15
- function waitFor(ms) {
16
- return new Promise((resolve) => setTimeout(resolve, ms));
62
+ // node_modules/.pnpm/scule@1.3.0/node_modules/scule/dist/index.mjs
63
+ var NUMBER_CHAR_RE = /\d/;
64
+ var STR_SPLITTERS = ["-", "_", "/", "."];
65
+ function isUppercase(char = "") {
66
+ if (NUMBER_CHAR_RE.test(char)) {
67
+ return void 0;
68
+ }
69
+ return char !== char.toLowerCase();
17
70
  }
18
-
19
- function toArray(val) {
20
- val = val ?? [];
21
- return Array.isArray(val) ? val : [val];
71
+ function splitByCase(str, separators) {
72
+ const splitters = separators ?? STR_SPLITTERS;
73
+ const parts = [];
74
+ if (!str || typeof str !== "string") {
75
+ return parts;
76
+ }
77
+ let buff = "";
78
+ let previousUpper;
79
+ let previousSplitter;
80
+ for (const char of str) {
81
+ const isSplitter = splitters.includes(char);
82
+ if (isSplitter === true) {
83
+ parts.push(buff);
84
+ buff = "";
85
+ previousUpper = void 0;
86
+ continue;
87
+ }
88
+ const isUpper = isUppercase(char);
89
+ if (previousSplitter === false) {
90
+ if (previousUpper === false && isUpper === true) {
91
+ parts.push(buff);
92
+ buff = char;
93
+ previousUpper = isUpper;
94
+ continue;
95
+ }
96
+ if (previousUpper === true && isUpper === false && buff.length > 1) {
97
+ const lastChar = buff.at(-1);
98
+ parts.push(buff.slice(0, Math.max(0, buff.length - 1)));
99
+ buff = lastChar + char;
100
+ previousUpper = isUpper;
101
+ continue;
102
+ }
103
+ }
104
+ buff += char;
105
+ previousUpper = isUpper;
106
+ previousSplitter = isSplitter;
107
+ }
108
+ parts.push(buff);
109
+ return parts;
110
+ }
111
+ function upperFirst(str) {
112
+ return str ? str[0].toUpperCase() + str.slice(1) : "";
113
+ }
114
+ function lowerFirst(str) {
115
+ return str ? str[0].toLowerCase() + str.slice(1) : "";
116
+ }
117
+ function pascalCase(str, opts) {
118
+ return str ? (Array.isArray(str) ? str : splitByCase(str)).map((p) => upperFirst(opts?.normalize ? p.toLowerCase() : p)).join("") : "";
119
+ }
120
+ function camelCase(str, opts) {
121
+ return lowerFirst(pascalCase(str || "", opts));
122
+ }
123
+ function kebabCase(str, joiner) {
124
+ return str ? (Array.isArray(str) ? str : splitByCase(str)).map((p) => p.toLowerCase()).join(joiner ?? "-") : "";
125
+ }
126
+ function snakeCase(str) {
127
+ return kebabCase(str || "", "_");
128
+ }
129
+ function flatCase(str) {
130
+ return kebabCase(str || "", "");
131
+ }
132
+ function trainCase(str, opts) {
133
+ return (Array.isArray(str) ? str : splitByCase(str)).filter(Boolean).map((p) => upperFirst(opts?.normalize ? p.toLowerCase() : p)).join("-");
134
+ }
135
+ var titleCaseExceptions = /^(a|an|and|as|at|but|by|for|if|in|is|nor|of|on|or|the|to|with)$/i;
136
+ function titleCase(str, opts) {
137
+ return (Array.isArray(str) ? str : splitByCase(str)).filter(Boolean).map(
138
+ (p) => titleCaseExceptions.test(p) ? p.toLowerCase() : upperFirst(opts?.normalize ? p.toLowerCase() : p)
139
+ ).join(" ");
22
140
  }
23
141
 
24
- const isBrowser = () => typeof document !== "undefined";
142
+ // src/case.ts
143
+ var capitalize = upperFirst;
25
144
 
26
- const root = isBrowser() ? window : global;
27
- let prev = Date.now();
145
+ // src/misc/raf.ts
146
+ var root = isBrowser() ? window : globalThis;
147
+ var prev = Date.now();
28
148
  function mockRAF(fn) {
29
149
  const curr = Date.now();
30
150
  const ms = Math.max(0, 16 - (curr - prev));
@@ -41,11 +161,97 @@ function cAF(id) {
41
161
  return caf.call(root, id);
42
162
  }
43
163
 
44
- exports.cAF = cAF;
45
- exports.camelCase = camelCase;
46
- exports.capitalize = capitalize;
47
- exports.isBrowser = isBrowser;
48
- exports.kekabCase = kekabCase;
49
- exports.rAF = rAF;
50
- exports.toArray = toArray;
51
- exports.waitFor = waitFor;
164
+ // src/misc/time.ts
165
+ var ONE_SECOND = 1e3;
166
+ var ONE_MINUTE = 60 * ONE_SECOND;
167
+ var ONE_HOUR = 60 * ONE_MINUTE;
168
+ var ONE_DAY = 24 * ONE_HOUR;
169
+ var ONE_WEEK = 7 * ONE_DAY;
170
+ function seconds(count) {
171
+ return count * ONE_SECOND;
172
+ }
173
+ function minutes(count) {
174
+ return count * ONE_MINUTE;
175
+ }
176
+ function hours(count) {
177
+ return count * ONE_HOUR;
178
+ }
179
+ function days(count) {
180
+ return count * ONE_DAY;
181
+ }
182
+ function weeks(count) {
183
+ return count * ONE_WEEK;
184
+ }
185
+
186
+ // src/misc/waitFor.ts
187
+ function waitFor(ms) {
188
+ return new Promise((resolve) => setTimeout(resolve, ms));
189
+ }
190
+
191
+ // src/misc/warnOnce.ts
192
+ var warned = /* @__PURE__ */ new Set();
193
+ var warnOnce = (message) => {
194
+ if (warned.has(message)) {
195
+ return;
196
+ }
197
+ warned.add(message);
198
+ console.warn(message);
199
+ };
200
+
201
+ // src/array/unique.ts
202
+ function unique(array) {
203
+ return Array.from(new Set(array));
204
+ }
205
+ function uniqueBy(array, equalFn) {
206
+ return array.reduce((acc, cur) => {
207
+ const idx = acc.findIndex((item) => equalFn(item, cur));
208
+ if (idx === -1) {
209
+ acc.push(cur);
210
+ }
211
+ return acc;
212
+ }, []);
213
+ }
214
+
215
+ // src/array/toArray.ts
216
+ function toArray(array) {
217
+ array = array ?? [];
218
+ return Array.isArray(array) ? array : [array];
219
+ }
220
+
221
+ // src/string/join.ts
222
+ function join(array, options = {}) {
223
+ const { separator = "" } = options;
224
+ if (!Array.isArray(array) || !array.length) return "";
225
+ return array.filter((v) => Boolean(v) || v === 0).join(separator);
226
+ }
227
+ // Annotate the CommonJS export names for ESM import in node:
228
+ 0 && (module.exports = {
229
+ NOOP,
230
+ cAF,
231
+ camelCase,
232
+ capitalize,
233
+ days,
234
+ flatCase,
235
+ hours,
236
+ isBrowser,
237
+ isUppercase,
238
+ join,
239
+ kebabCase,
240
+ lowerFirst,
241
+ minutes,
242
+ noop,
243
+ pascalCase,
244
+ rAF,
245
+ seconds,
246
+ snakeCase,
247
+ splitByCase,
248
+ titleCase,
249
+ toArray,
250
+ trainCase,
251
+ unique,
252
+ uniqueBy,
253
+ upperFirst,
254
+ waitFor,
255
+ warnOnce,
256
+ weeks
257
+ });
package/dist/index.d.cts CHANGED
@@ -1,46 +1,61 @@
1
- import { Nullable, Arrayable } from '@ntnyq/types';
1
+ import { upperFirst } from 'scule';
2
+ export * from 'scule';
2
3
 
3
4
  /**
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
- * ```
5
+ * A function that does nothing.
6
+ */
7
+ declare const noop: () => void;
8
+ /**
9
+ * Alias of {@link noop}.
14
10
  */
15
- declare function kekabCase(input: string): string;
11
+ declare const NOOP: () => void;
16
12
 
17
13
  /**
18
- * transform given string to camelCase
19
- *
20
- * @param input given string
21
- * @returns string in camelCase
14
+ * @file env.ts
15
+ */
16
+ /**
17
+ * Checks if the code is running in a browser
22
18
  *
23
- * @example
24
- * ```
25
- * import { camelCase } from '@ntnyq/utils'
26
- * camelCase('foo-bar-baz') // fooBarBaz
27
- * ```
19
+ * @returns boolean - true if the code is running in a browser
20
+ */
21
+ declare const isBrowser: () => boolean;
22
+
23
+ /**
24
+ * @file case utils
25
+ * @category Case
28
26
  */
29
- declare function camelCase(input: string): string;
30
27
 
31
28
  /**
32
- * capitalize the given string's first letter
29
+ * @deprecated use upperFirst instead
30
+ */
31
+ declare const capitalize: typeof upperFirst;
32
+
33
+ /**
34
+ * @file raf.ts
35
+ */
36
+ /**
37
+ * Request animation frame
33
38
  *
34
- * @param input given string
35
- * @returns string first letter capitalize
39
+ * @param fn callback
40
+ * @returns id
41
+ */
42
+ declare function rAF(fn: FrameRequestCallback): number;
43
+ /**
44
+ * Cancel animation frame
36
45
  *
37
- * @example
38
- * ```
39
- * import { capitalize } from '@ntnyq/utils'
40
- * capitalize('fooBarBaz') // FooBarBaz
41
- * ```
46
+ * @param id id
47
+ * @returns void
48
+ */
49
+ declare function cAF(id: number): void;
50
+
51
+ /**
52
+ * @category Utils - time
42
53
  */
43
- declare function capitalize(input: string): string;
54
+ declare function seconds(count: number): number;
55
+ declare function minutes(count: number): number;
56
+ declare function hours(count: number): number;
57
+ declare function days(count: number): number;
58
+ declare function weeks(count: number): number;
44
59
 
45
60
  /**
46
61
  * Wait for a number of milliseconds
@@ -57,34 +72,49 @@ declare function capitalize(input: string): string;
57
72
  */
58
73
  declare function waitFor(ms: number): Promise<unknown>;
59
74
 
60
- declare function toArray<T>(val?: Nullable<Arrayable<T>>): Arrayable<T>;
75
+ declare const warnOnce: (message: string) => void;
61
76
 
62
77
  /**
63
- * @file env.ts
78
+ * Returns a new array with unique values.
79
+ * @param array - The array to process.
80
+ * @returns The new array.
64
81
  */
82
+ declare function unique<T>(array: T[]): T[];
65
83
  /**
66
- * Checks if the code is running in a browser
67
- *
68
- * @returns boolean - true if the code is running in a browser
84
+ * Returns a new array with unique values.
85
+ * @param array - The array to process.
86
+ * @param equalFn - The function to compare values.
87
+ * @returns The new array.
69
88
  */
70
- declare const isBrowser: () => boolean;
89
+ declare function uniqueBy<T>(array: T[], equalFn: (a: T, b: T) => boolean): T[];
90
+
91
+ type Nullable<T> = T | null;
92
+ type MayBe<T> = T | undefined;
93
+ type AnyFn<T = any, R = any> = (...args: T[]) => R;
94
+ type Arrayable<T> = T | T[];
95
+ type Awaitable<T> = T | Promise<T>;
96
+ type Prettify<T> = Omit<T, never>;
71
97
 
72
98
  /**
73
- * @file raf.ts
74
- */
75
- /**
76
- * Request animation frame
77
- *
78
- * @param fn callback
79
- * @returns id
99
+ * Converts a value to an array.
100
+ * @param array - The value to convert.
101
+ * @returns The array.
80
102
  */
81
- declare function rAF(fn: FrameRequestCallback): number;
103
+ declare function toArray<T>(array?: Nullable<Arrayable<T>>): T[];
104
+
105
+ type JoinableValue = string | number | null | undefined;
106
+ interface JoinOptions {
107
+ /**
108
+ * @default '''
109
+ */
110
+ separator?: string;
111
+ }
82
112
  /**
83
- * Cancel animation frame
84
- *
85
- * @param id id
86
- * @returns void
113
+ * Joins an array of strings or numbers into a single string.
114
+ * @param array - An array of strings or numbers.
115
+ * @param options - An object of options.
116
+ * @returns A string.
87
117
  */
88
- declare function cAF(id: number): void;
118
+ declare function join(array: JoinableValue[], options?: JoinOptions): string;
89
119
 
90
- export { cAF, camelCase, capitalize, isBrowser, kekabCase, rAF, toArray, waitFor };
120
+ export { type AnyFn, type Arrayable, type Awaitable, type MayBe, NOOP, type Nullable, type Prettify, cAF, capitalize, days, hours, isBrowser, join, minutes, noop, rAF, seconds, toArray, unique, uniqueBy, waitFor, warnOnce, weeks };
package/dist/index.d.ts CHANGED
@@ -1,46 +1,61 @@
1
- import { Nullable, Arrayable } from '@ntnyq/types';
1
+ import { upperFirst } from 'scule';
2
+ export * from 'scule';
2
3
 
3
4
  /**
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
- * ```
5
+ * A function that does nothing.
6
+ */
7
+ declare const noop: () => void;
8
+ /**
9
+ * Alias of {@link noop}.
14
10
  */
15
- declare function kekabCase(input: string): string;
11
+ declare const NOOP: () => void;
16
12
 
17
13
  /**
18
- * transform given string to camelCase
19
- *
20
- * @param input given string
21
- * @returns string in camelCase
14
+ * @file env.ts
15
+ */
16
+ /**
17
+ * Checks if the code is running in a browser
22
18
  *
23
- * @example
24
- * ```
25
- * import { camelCase } from '@ntnyq/utils'
26
- * camelCase('foo-bar-baz') // fooBarBaz
27
- * ```
19
+ * @returns boolean - true if the code is running in a browser
20
+ */
21
+ declare const isBrowser: () => boolean;
22
+
23
+ /**
24
+ * @file case utils
25
+ * @category Case
28
26
  */
29
- declare function camelCase(input: string): string;
30
27
 
31
28
  /**
32
- * capitalize the given string's first letter
29
+ * @deprecated use upperFirst instead
30
+ */
31
+ declare const capitalize: typeof upperFirst;
32
+
33
+ /**
34
+ * @file raf.ts
35
+ */
36
+ /**
37
+ * Request animation frame
33
38
  *
34
- * @param input given string
35
- * @returns string first letter capitalize
39
+ * @param fn callback
40
+ * @returns id
41
+ */
42
+ declare function rAF(fn: FrameRequestCallback): number;
43
+ /**
44
+ * Cancel animation frame
36
45
  *
37
- * @example
38
- * ```
39
- * import { capitalize } from '@ntnyq/utils'
40
- * capitalize('fooBarBaz') // FooBarBaz
41
- * ```
46
+ * @param id id
47
+ * @returns void
48
+ */
49
+ declare function cAF(id: number): void;
50
+
51
+ /**
52
+ * @category Utils - time
42
53
  */
43
- declare function capitalize(input: string): string;
54
+ declare function seconds(count: number): number;
55
+ declare function minutes(count: number): number;
56
+ declare function hours(count: number): number;
57
+ declare function days(count: number): number;
58
+ declare function weeks(count: number): number;
44
59
 
45
60
  /**
46
61
  * Wait for a number of milliseconds
@@ -57,34 +72,49 @@ declare function capitalize(input: string): string;
57
72
  */
58
73
  declare function waitFor(ms: number): Promise<unknown>;
59
74
 
60
- declare function toArray<T>(val?: Nullable<Arrayable<T>>): Arrayable<T>;
75
+ declare const warnOnce: (message: string) => void;
61
76
 
62
77
  /**
63
- * @file env.ts
78
+ * Returns a new array with unique values.
79
+ * @param array - The array to process.
80
+ * @returns The new array.
64
81
  */
82
+ declare function unique<T>(array: T[]): T[];
65
83
  /**
66
- * Checks if the code is running in a browser
67
- *
68
- * @returns boolean - true if the code is running in a browser
84
+ * Returns a new array with unique values.
85
+ * @param array - The array to process.
86
+ * @param equalFn - The function to compare values.
87
+ * @returns The new array.
69
88
  */
70
- declare const isBrowser: () => boolean;
89
+ declare function uniqueBy<T>(array: T[], equalFn: (a: T, b: T) => boolean): T[];
90
+
91
+ type Nullable<T> = T | null;
92
+ type MayBe<T> = T | undefined;
93
+ type AnyFn<T = any, R = any> = (...args: T[]) => R;
94
+ type Arrayable<T> = T | T[];
95
+ type Awaitable<T> = T | Promise<T>;
96
+ type Prettify<T> = Omit<T, never>;
71
97
 
72
98
  /**
73
- * @file raf.ts
74
- */
75
- /**
76
- * Request animation frame
77
- *
78
- * @param fn callback
79
- * @returns id
99
+ * Converts a value to an array.
100
+ * @param array - The value to convert.
101
+ * @returns The array.
80
102
  */
81
- declare function rAF(fn: FrameRequestCallback): number;
103
+ declare function toArray<T>(array?: Nullable<Arrayable<T>>): T[];
104
+
105
+ type JoinableValue = string | number | null | undefined;
106
+ interface JoinOptions {
107
+ /**
108
+ * @default '''
109
+ */
110
+ separator?: string;
111
+ }
82
112
  /**
83
- * Cancel animation frame
84
- *
85
- * @param id id
86
- * @returns void
113
+ * Joins an array of strings or numbers into a single string.
114
+ * @param array - An array of strings or numbers.
115
+ * @param options - An object of options.
116
+ * @returns A string.
87
117
  */
88
- declare function cAF(id: number): void;
118
+ declare function join(array: JoinableValue[], options?: JoinOptions): string;
89
119
 
90
- export { cAF, camelCase, capitalize, isBrowser, kekabCase, rAF, toArray, waitFor };
120
+ export { type AnyFn, type Arrayable, type Awaitable, type MayBe, NOOP, type Nullable, type Prettify, cAF, capitalize, days, hours, isBrowser, join, minutes, noop, rAF, seconds, toArray, unique, uniqueBy, waitFor, warnOnce, weeks };
package/dist/index.js ADDED
@@ -0,0 +1,203 @@
1
+ // src/fn/noop.ts
2
+ var noop = () => {
3
+ };
4
+ var NOOP = noop;
5
+
6
+ // src/env/isBrowser.ts
7
+ var isBrowser = () => typeof document !== "undefined";
8
+
9
+ // node_modules/.pnpm/scule@1.3.0/node_modules/scule/dist/index.mjs
10
+ var NUMBER_CHAR_RE = /\d/;
11
+ var STR_SPLITTERS = ["-", "_", "/", "."];
12
+ function isUppercase(char = "") {
13
+ if (NUMBER_CHAR_RE.test(char)) {
14
+ return void 0;
15
+ }
16
+ return char !== char.toLowerCase();
17
+ }
18
+ function splitByCase(str, separators) {
19
+ const splitters = separators ?? STR_SPLITTERS;
20
+ const parts = [];
21
+ if (!str || typeof str !== "string") {
22
+ return parts;
23
+ }
24
+ let buff = "";
25
+ let previousUpper;
26
+ let previousSplitter;
27
+ for (const char of str) {
28
+ const isSplitter = splitters.includes(char);
29
+ if (isSplitter === true) {
30
+ parts.push(buff);
31
+ buff = "";
32
+ previousUpper = void 0;
33
+ continue;
34
+ }
35
+ const isUpper = isUppercase(char);
36
+ if (previousSplitter === false) {
37
+ if (previousUpper === false && isUpper === true) {
38
+ parts.push(buff);
39
+ buff = char;
40
+ previousUpper = isUpper;
41
+ continue;
42
+ }
43
+ if (previousUpper === true && isUpper === false && buff.length > 1) {
44
+ const lastChar = buff.at(-1);
45
+ parts.push(buff.slice(0, Math.max(0, buff.length - 1)));
46
+ buff = lastChar + char;
47
+ previousUpper = isUpper;
48
+ continue;
49
+ }
50
+ }
51
+ buff += char;
52
+ previousUpper = isUpper;
53
+ previousSplitter = isSplitter;
54
+ }
55
+ parts.push(buff);
56
+ return parts;
57
+ }
58
+ function upperFirst(str) {
59
+ return str ? str[0].toUpperCase() + str.slice(1) : "";
60
+ }
61
+ function lowerFirst(str) {
62
+ return str ? str[0].toLowerCase() + str.slice(1) : "";
63
+ }
64
+ function pascalCase(str, opts) {
65
+ return str ? (Array.isArray(str) ? str : splitByCase(str)).map((p) => upperFirst(opts?.normalize ? p.toLowerCase() : p)).join("") : "";
66
+ }
67
+ function camelCase(str, opts) {
68
+ return lowerFirst(pascalCase(str || "", opts));
69
+ }
70
+ function kebabCase(str, joiner) {
71
+ return str ? (Array.isArray(str) ? str : splitByCase(str)).map((p) => p.toLowerCase()).join(joiner ?? "-") : "";
72
+ }
73
+ function snakeCase(str) {
74
+ return kebabCase(str || "", "_");
75
+ }
76
+ function flatCase(str) {
77
+ return kebabCase(str || "", "");
78
+ }
79
+ function trainCase(str, opts) {
80
+ return (Array.isArray(str) ? str : splitByCase(str)).filter(Boolean).map((p) => upperFirst(opts?.normalize ? p.toLowerCase() : p)).join("-");
81
+ }
82
+ var titleCaseExceptions = /^(a|an|and|as|at|but|by|for|if|in|is|nor|of|on|or|the|to|with)$/i;
83
+ function titleCase(str, opts) {
84
+ return (Array.isArray(str) ? str : splitByCase(str)).filter(Boolean).map(
85
+ (p) => titleCaseExceptions.test(p) ? p.toLowerCase() : upperFirst(opts?.normalize ? p.toLowerCase() : p)
86
+ ).join(" ");
87
+ }
88
+
89
+ // src/case.ts
90
+ var capitalize = upperFirst;
91
+
92
+ // src/misc/raf.ts
93
+ var root = isBrowser() ? window : globalThis;
94
+ var prev = Date.now();
95
+ function mockRAF(fn) {
96
+ const curr = Date.now();
97
+ const ms = Math.max(0, 16 - (curr - prev));
98
+ const id = setTimeout(fn, ms);
99
+ prev = curr + ms;
100
+ return id;
101
+ }
102
+ function rAF(fn) {
103
+ const raf = root.requestAnimationFrame || mockRAF;
104
+ return raf.call(root, fn);
105
+ }
106
+ function cAF(id) {
107
+ const caf = root.cancelAnimationFrame || root.clearTimeout;
108
+ return caf.call(root, id);
109
+ }
110
+
111
+ // src/misc/time.ts
112
+ var ONE_SECOND = 1e3;
113
+ var ONE_MINUTE = 60 * ONE_SECOND;
114
+ var ONE_HOUR = 60 * ONE_MINUTE;
115
+ var ONE_DAY = 24 * ONE_HOUR;
116
+ var ONE_WEEK = 7 * ONE_DAY;
117
+ function seconds(count) {
118
+ return count * ONE_SECOND;
119
+ }
120
+ function minutes(count) {
121
+ return count * ONE_MINUTE;
122
+ }
123
+ function hours(count) {
124
+ return count * ONE_HOUR;
125
+ }
126
+ function days(count) {
127
+ return count * ONE_DAY;
128
+ }
129
+ function weeks(count) {
130
+ return count * ONE_WEEK;
131
+ }
132
+
133
+ // src/misc/waitFor.ts
134
+ function waitFor(ms) {
135
+ return new Promise((resolve) => setTimeout(resolve, ms));
136
+ }
137
+
138
+ // src/misc/warnOnce.ts
139
+ var warned = /* @__PURE__ */ new Set();
140
+ var warnOnce = (message) => {
141
+ if (warned.has(message)) {
142
+ return;
143
+ }
144
+ warned.add(message);
145
+ console.warn(message);
146
+ };
147
+
148
+ // src/array/unique.ts
149
+ function unique(array) {
150
+ return Array.from(new Set(array));
151
+ }
152
+ function uniqueBy(array, equalFn) {
153
+ return array.reduce((acc, cur) => {
154
+ const idx = acc.findIndex((item) => equalFn(item, cur));
155
+ if (idx === -1) {
156
+ acc.push(cur);
157
+ }
158
+ return acc;
159
+ }, []);
160
+ }
161
+
162
+ // src/array/toArray.ts
163
+ function toArray(array) {
164
+ array = array ?? [];
165
+ return Array.isArray(array) ? array : [array];
166
+ }
167
+
168
+ // src/string/join.ts
169
+ function join(array, options = {}) {
170
+ const { separator = "" } = options;
171
+ if (!Array.isArray(array) || !array.length) return "";
172
+ return array.filter((v) => Boolean(v) || v === 0).join(separator);
173
+ }
174
+ export {
175
+ NOOP,
176
+ cAF,
177
+ camelCase,
178
+ capitalize,
179
+ days,
180
+ flatCase,
181
+ hours,
182
+ isBrowser,
183
+ isUppercase,
184
+ join,
185
+ kebabCase,
186
+ lowerFirst,
187
+ minutes,
188
+ noop,
189
+ pascalCase,
190
+ rAF,
191
+ seconds,
192
+ snakeCase,
193
+ splitByCase,
194
+ titleCase,
195
+ toArray,
196
+ trainCase,
197
+ unique,
198
+ uniqueBy,
199
+ upperFirst,
200
+ waitFor,
201
+ warnOnce,
202
+ weeks
203
+ };
package/package.json CHANGED
@@ -1,28 +1,27 @@
1
1
  {
2
2
  "name": "@ntnyq/utils",
3
- "version": "0.0.3",
4
- "publishConfig": {
5
- "access": "public"
6
- },
7
- "description": "",
8
- "keywords": [],
3
+ "type": "module",
4
+ "version": "0.1.1",
5
+ "description": "Common used utils.",
6
+ "keywords": [
7
+ "utils"
8
+ ],
9
9
  "license": "MIT",
10
10
  "author": {
11
11
  "name": "ntnyq",
12
12
  "email": "ntnyq13@gmail.com"
13
13
  },
14
- "homepage": "https://github.com/ntnyq/ntnyq-utils#readme",
15
- "repository": {
16
- "type": "git",
17
- "url": "ntnyq/ntnyq-utils",
18
- "directory": "packages/utils"
14
+ "homepage": "https://github.com/ntnyq/utils#readme",
15
+ "bugs": {
16
+ "url": "https://github.com/ntnyq/utils/issues"
19
17
  },
18
+ "repository": "ntnyq/utils",
20
19
  "exports": {
21
20
  "./package.json": "./package.json",
22
21
  ".": {
23
22
  "import": {
24
- "types": "./dist/index.d.mts",
25
- "default": "./dist/index.mjs"
23
+ "types": "./dist/index.d.ts",
24
+ "default": "./dist/index.js"
26
25
  },
27
26
  "require": {
28
27
  "types": "./dist/index.d.cts",
@@ -30,29 +29,47 @@
30
29
  }
31
30
  }
32
31
  },
33
- "main": "./dist/index.cjs",
34
- "module": "./dist/index.mjs",
32
+ "main": "./dist/index.js",
33
+ "module": "./dist/index.js",
35
34
  "types": "./dist/index.d.ts",
36
- "typesVersions": {
37
- "*": {
38
- "*": [
39
- "./dist/*",
40
- "./dist/index.d.ts"
41
- ]
42
- }
43
- },
44
35
  "files": [
45
36
  "dist"
46
37
  ],
47
38
  "sideEffects": false,
48
- "bugs": {
49
- "url": "https://github.com/ntnyq/ntnyq-utils/issues"
50
- },
51
39
  "dependencies": {
52
- "@ntnyq/types": "0.0.2"
40
+ "scule": "^1.3.0"
41
+ },
42
+ "devDependencies": {
43
+ "@ntnyq/eslint-config": "^3.0.0-beta.17",
44
+ "@ntnyq/prettier-config": "^1.21.3",
45
+ "@vitest/coverage-v8": "^2.1.1",
46
+ "bumpp": "^9.5.2",
47
+ "eslint": "^9.11.1",
48
+ "husky": "^9.1.6",
49
+ "nano-staged": "^0.8.0",
50
+ "npm-run-all2": "^6.2.3",
51
+ "pnpm": "^9.11.0",
52
+ "prettier": "^3.3.3",
53
+ "tsup": "^8.3.0",
54
+ "typescript": "^5.6.2",
55
+ "vitest": "^2.1.1"
56
+ },
57
+ "engines": {
58
+ "node": ">=18.18.0"
59
+ },
60
+ "prettier": "@ntnyq/prettier-config",
61
+ "nano-staged": {
62
+ "*.{js,ts,mjs,cjs,json,md,yml,yaml}": "eslint --fix"
53
63
  },
54
64
  "scripts": {
55
- "build": "unbuild",
56
- "dev": "unbuild --stub"
65
+ "build": "tsup",
66
+ "coverage": "vitest --coverage",
67
+ "dev": "tsup --watch src",
68
+ "lint": "eslint .",
69
+ "release": "run-s release:check release:publish",
70
+ "release:check": "run-s lint typecheck test",
71
+ "release:publish": "bumpp && pnpm publish",
72
+ "test": "vitest",
73
+ "typecheck": "tsc --noEmit"
57
74
  }
58
75
  }
package/dist/index.d.mts DELETED
@@ -1,90 +0,0 @@
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.mjs DELETED
@@ -1,42 +0,0 @@
1
- function kekabCase(input) {
2
- return input.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase();
3
- }
4
-
5
- function camelCase(input) {
6
- return input.replace(/[._-](\w)/g, (_, c) => c ? c.toUpperCase() : "");
7
- }
8
-
9
- function capitalize(input) {
10
- return `${input.charAt(0).toUpperCase()}${input.slice(1)}`;
11
- }
12
-
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 };