@poppinss/utils 6.5.0-4 → 6.5.0-6

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.
@@ -0,0 +1,269 @@
1
+ import {
2
+ base64,
3
+ milliseconds_default
4
+ } from "./chunk-PWRXO3NF.js";
5
+
6
+ // src/string/bytes.ts
7
+ import bytes from "bytes";
8
+ var bytes_default = {
9
+ format(valueInBytes, options) {
10
+ return bytes.format(valueInBytes, options);
11
+ },
12
+ /**
13
+ * Parse the unit expression to bytes. If the unit value
14
+ * is a number, then it will be returned as it is considering
15
+ * it is already in bytes.
16
+ */
17
+ parse(unit) {
18
+ if (typeof unit === "number") {
19
+ return unit;
20
+ }
21
+ return bytes.parse(unit);
22
+ }
23
+ };
24
+
25
+ // src/string/seconds.ts
26
+ import { parse, format } from "@lukeed/ms";
27
+ var seconds_default = {
28
+ format(seconds, long) {
29
+ return format(seconds * 1e3, long);
30
+ },
31
+ /**
32
+ * Parse time expression string to seconds. The number value
33
+ * is returned as it is, considering it is already in seconds
34
+ */
35
+ parse(duration) {
36
+ if (typeof duration === "number") {
37
+ return duration;
38
+ }
39
+ const milliseconds = parse(duration);
40
+ if (milliseconds === void 0) {
41
+ throw new Error(`Invalid duration expression "${duration}"`);
42
+ }
43
+ return Math.floor(milliseconds / 1e3);
44
+ }
45
+ };
46
+
47
+ // src/string/slugify.ts
48
+ import { default as slugifyPkg } from "slugify";
49
+ var slug = slugifyPkg;
50
+
51
+ // src/string/random.ts
52
+ import { randomBytes } from "node:crypto";
53
+ function random(size) {
54
+ const bits = (size + 1) * 6;
55
+ const buffer = randomBytes(Math.ceil(bits / 8));
56
+ return base64.urlEncode(buffer).slice(0, size);
57
+ }
58
+
59
+ // src/string/excerpt.ts
60
+ import truncatise from "truncatise";
61
+ function excerpt(sentence2, charactersLimit, options) {
62
+ return truncatise(sentence2, {
63
+ TruncateLength: charactersLimit,
64
+ /**
65
+ * Do not complete words when "completeWords" is not explicitly set
66
+ * to true
67
+ */
68
+ Strict: options && options.completeWords === true ? false : true,
69
+ StripHTML: true,
70
+ TruncateBy: "characters",
71
+ Suffix: options && options.suffix
72
+ });
73
+ }
74
+
75
+ // src/string/ordinal.ts
76
+ function ordinal(value) {
77
+ const transformedValue = Math.abs(typeof value === "string" ? Number.parseInt(value) : value);
78
+ if (!Number.isFinite(transformedValue) || Number.isNaN(transformedValue)) {
79
+ throw new Error("Cannot ordinalize invalid or infinite numbers");
80
+ }
81
+ const percent = transformedValue % 100;
82
+ if (percent >= 10 && percent <= 20) {
83
+ return `${value}th`;
84
+ }
85
+ const decimal = transformedValue % 10;
86
+ switch (decimal) {
87
+ case 1:
88
+ return `${value}st`;
89
+ case 2:
90
+ return `${value}nd`;
91
+ case 3:
92
+ return `${value}rd`;
93
+ default:
94
+ return `${value}th`;
95
+ }
96
+ }
97
+
98
+ // src/string/truncate.ts
99
+ import truncatise2 from "truncatise";
100
+ function truncate(sentence2, charactersLimit, options) {
101
+ return truncatise2(sentence2, {
102
+ TruncateLength: charactersLimit,
103
+ /**
104
+ * Do not complete words when "completeWords" is not explicitly set
105
+ * to true
106
+ */
107
+ Strict: options && options.completeWords === true ? false : true,
108
+ StripHTML: false,
109
+ TruncateBy: "characters",
110
+ Suffix: options && options.suffix
111
+ });
112
+ }
113
+
114
+ // src/string/sentence.ts
115
+ function sentence(values, options) {
116
+ if (values.length === 0) {
117
+ return "";
118
+ }
119
+ if (values.length === 1) {
120
+ return values[0];
121
+ }
122
+ if (values.length === 2) {
123
+ return `${values[0]}${options?.pairSeparator || " and "}${values[1]}`;
124
+ }
125
+ const normalized = Object.assign({ separator: ", ", lastSeparator: ", and " }, options);
126
+ return `${values.slice(0, -1).join(normalized.separator)}${normalized.lastSeparator}${values[values.length - 1]}`;
127
+ }
128
+
129
+ // src/string/interpolate.ts
130
+ function parseProp(data, key) {
131
+ const tokens = key.split(".");
132
+ while (tokens.length) {
133
+ if (data === null || typeof data !== "object") {
134
+ return;
135
+ }
136
+ const token = tokens.shift();
137
+ data = Object.hasOwn(data, token) ? data[token] : void 0;
138
+ }
139
+ return data;
140
+ }
141
+ function interpolate(input, data) {
142
+ return input.replace(/(\\)?{{(.*?)}}/g, (_, escapeChar, key) => {
143
+ if (escapeChar) {
144
+ return `{{${key}}}`;
145
+ }
146
+ return parseProp(data, key.trim());
147
+ });
148
+ }
149
+
150
+ // src/string/pluralize.ts
151
+ import { default as pluralizePkg } from "pluralize";
152
+ function pluralize(word, count, inclusive) {
153
+ return pluralizePkg(word, count, inclusive);
154
+ }
155
+ pluralize.addPluralRule = pluralizePkg.addPluralRule;
156
+ pluralize.addSingularRule = pluralizePkg.addSingularRule;
157
+ pluralize.addIrregularRule = pluralizePkg.addIrregularRule;
158
+ pluralize.addUncountableRule = pluralizePkg.addUncountableRule;
159
+ var plural = pluralizePkg.plural;
160
+ var singular = pluralizePkg.singular;
161
+ var isPlural = pluralizePkg.isPlural;
162
+ var isSingular = pluralizePkg.isSingular;
163
+
164
+ // src/string/change_case.ts
165
+ import * as changeCase from "case-anything";
166
+ var NO_CASE_SPLIT_REGEXP = [/([a-z0-9])([A-Z])/g, /([A-Z])([A-Z][a-z])/g];
167
+ var NO_CASE_STRIP_REGEXP = /[^A-Z0-9]+/gi;
168
+ var SMALL_WORDS = /\b(?:an?d?|a[st]|because|but|by|en|for|i[fn]|neither|nor|o[fnr]|only|over|per|so|some|tha[tn]|the|to|up|upon|vs?\.?|versus|via|when|with|without|yet)\b/i;
169
+ var TOKENS = /[^\s:–—-]+|./g;
170
+ var WHITESPACE = /\s/;
171
+ var IS_MANUAL_CASE = /.(?=[A-Z]|\..)/;
172
+ var ALPHANUMERIC_PATTERN = /[A-Za-z0-9\u00C0-\u00FF]/;
173
+ function titleCase(input) {
174
+ let output = "";
175
+ let result;
176
+ while ((result = TOKENS.exec(input)) !== null) {
177
+ const { 0: token, index } = result;
178
+ if (!IS_MANUAL_CASE.test(token) && (!SMALL_WORDS.test(token) || index === 0 || index + token.length === input.length) && (input.charAt(index + token.length) !== ":" || WHITESPACE.test(input.charAt(index + token.length + 1)))) {
179
+ output += token.replace(ALPHANUMERIC_PATTERN, (char) => char.toUpperCase());
180
+ continue;
181
+ }
182
+ output += token;
183
+ }
184
+ return output;
185
+ }
186
+ function camelCase2(value) {
187
+ return changeCase.camelCase(value);
188
+ }
189
+ function snakeCase2(value) {
190
+ return changeCase.snakeCase(value);
191
+ }
192
+ function dashCase(value, options) {
193
+ if (options && options.capitalize) {
194
+ return changeCase.trainCase(value);
195
+ }
196
+ return changeCase.kebabCase(value);
197
+ }
198
+ function pascalCase2(value) {
199
+ return changeCase.pascalCase(value);
200
+ }
201
+ function capitalCase2(value) {
202
+ return changeCase.capitalCase(value);
203
+ }
204
+ function sentenceCase(value) {
205
+ return noCase(value, (input, index) => {
206
+ const result = input.toLowerCase();
207
+ if (index === 0) {
208
+ return input.charAt(0).toUpperCase() + input.substring(1);
209
+ }
210
+ return result;
211
+ });
212
+ }
213
+ function dotCase(value, options) {
214
+ const transformedValue = changeCase.dotNotation(value);
215
+ if (options && options.lowerCase) {
216
+ return transformedValue.toLowerCase();
217
+ }
218
+ return transformedValue;
219
+ }
220
+ function noCase(value, transform) {
221
+ let result = NO_CASE_SPLIT_REGEXP.reduce((input, regex) => input.replace(regex, "$1\0$2"), value);
222
+ result = result.replace(NO_CASE_STRIP_REGEXP, "\0");
223
+ let start = 0;
224
+ let end = result.length;
225
+ while (result.charAt(start) === "\0") {
226
+ start++;
227
+ }
228
+ while (result.charAt(end - 1) === "\0") {
229
+ end--;
230
+ }
231
+ return result.slice(start, end).split("\0").map(transform || ((input) => input.toLowerCase())).join(" ");
232
+ }
233
+
234
+ // src/string/main.ts
235
+ function condenseWhitespace(value) {
236
+ return value.trim().replace(/\s{2,}/g, " ");
237
+ }
238
+ var string = {
239
+ excerpt,
240
+ truncate,
241
+ slug,
242
+ interpolate,
243
+ plural,
244
+ pluralize,
245
+ singular,
246
+ isPlural,
247
+ isSingular,
248
+ camelCase: camelCase2,
249
+ capitalCase: capitalCase2,
250
+ dashCase,
251
+ dotCase,
252
+ noCase,
253
+ pascalCase: pascalCase2,
254
+ sentenceCase,
255
+ snakeCase: snakeCase2,
256
+ titleCase,
257
+ random,
258
+ sentence,
259
+ condenseWhitespace,
260
+ seconds: seconds_default,
261
+ milliseconds: milliseconds_default,
262
+ bytes: bytes_default,
263
+ ordinal
264
+ };
265
+ var main_default = string;
266
+
267
+ export {
268
+ main_default
269
+ };
@@ -0,0 +1,63 @@
1
+ // src/base64.ts
2
+ var Base64 = class {
3
+ encode(data, encoding) {
4
+ if (typeof data === "string") {
5
+ return Buffer.from(data, encoding).toString("base64");
6
+ }
7
+ return Buffer.from(data).toString("base64");
8
+ }
9
+ decode(encoded, encoding = "utf-8", strict = false) {
10
+ if (Buffer.isBuffer(encoded)) {
11
+ return encoded.toString(encoding);
12
+ }
13
+ const decoded = Buffer.from(encoded, "base64").toString(encoding);
14
+ const isInvalid = this.encode(decoded, encoding) !== encoded;
15
+ if (strict && isInvalid) {
16
+ throw new Error("Cannot decode malformed value");
17
+ }
18
+ return isInvalid ? null : decoded;
19
+ }
20
+ urlEncode(data, encoding) {
21
+ const encoded = typeof data === "string" ? this.encode(data, encoding) : this.encode(data);
22
+ return encoded.replace(/\+/g, "-").replace(/\//g, "_").replace(/\=/g, "");
23
+ }
24
+ urlDecode(encoded, encoding = "utf-8", strict = false) {
25
+ if (Buffer.isBuffer(encoded)) {
26
+ return encoded.toString(encoding);
27
+ }
28
+ const decoded = Buffer.from(encoded, "base64").toString(encoding);
29
+ const isInvalid = this.urlEncode(decoded, encoding) !== encoded;
30
+ if (strict && isInvalid) {
31
+ throw new Error("Cannot urlDecode malformed value");
32
+ }
33
+ return isInvalid ? null : decoded;
34
+ }
35
+ };
36
+ var base64 = new Base64();
37
+
38
+ // src/string/milliseconds.ts
39
+ import { parse, format } from "@lukeed/ms";
40
+ var milliseconds_default = {
41
+ format(milliseconds, long) {
42
+ return format(milliseconds, long);
43
+ },
44
+ /**
45
+ * Parse time expression string to milliseconds. The number value
46
+ * is returned as it is, considering it is already in milliseconds
47
+ */
48
+ parse(duration) {
49
+ if (typeof duration === "number") {
50
+ return duration;
51
+ }
52
+ const milliseconds = parse(duration);
53
+ if (milliseconds === void 0) {
54
+ throw new Error(`Invalid duration expression "${duration}"`);
55
+ }
56
+ return milliseconds;
57
+ }
58
+ };
59
+
60
+ export {
61
+ base64,
62
+ milliseconds_default
63
+ };
@@ -0,0 +1,39 @@
1
+ // src/json/safe_parse.ts
2
+ import { parse } from "secure-json-parse";
3
+ function safeParse(jsonString, reviver) {
4
+ return parse(jsonString, reviver, {
5
+ protoAction: "remove",
6
+ constructorAction: "remove"
7
+ });
8
+ }
9
+
10
+ // src/json/safe_stringify.ts
11
+ import { configure } from "safe-stable-stringify";
12
+ var stringify = configure({
13
+ bigint: false,
14
+ circularValue: void 0,
15
+ deterministic: false
16
+ });
17
+ function jsonStringifyReplacer(replacer) {
18
+ return function(key, value) {
19
+ const val = replacer ? replacer.call(this, key, value) : value;
20
+ if (typeof val === "bigint") {
21
+ return val.toString();
22
+ }
23
+ return val;
24
+ };
25
+ }
26
+ function safeStringify(value, replacer, space) {
27
+ return stringify(value, jsonStringifyReplacer(replacer), space);
28
+ }
29
+
30
+ // src/json/main.ts
31
+ var json = {
32
+ safeParse,
33
+ safeStringify
34
+ };
35
+ var main_default = json;
36
+
37
+ export {
38
+ main_default
39
+ };
package/package.json CHANGED
@@ -1,15 +1,12 @@
1
1
  {
2
2
  "name": "@poppinss/utils",
3
- "version": "6.5.0-4",
3
+ "version": "6.5.0-6",
4
4
  "description": "Handy utilities for repetitive work",
5
5
  "main": "build/index.js",
6
6
  "type": "module",
7
7
  "files": [
8
- "build/src",
9
- "lodash",
10
- "build/lodash",
11
- "build/index.d.ts",
12
- "build/index.js"
8
+ "build",
9
+ "lodash"
13
10
  ],
14
11
  "exports": {
15
12
  ".": "./build/index.js",
@@ -17,6 +14,7 @@
17
14
  "types": "./lodash/lodash.types.d.ts",
18
15
  "node": "./build/lodash/main.cjs"
19
16
  },
17
+ "./assert": "./build/src/assert.js",
20
18
  "./string": "./build/src/string/main.js",
21
19
  "./string_builder": "./build/src/string_builder.js",
22
20
  "./json": "./build/src/json/main.js",