@luxass/utils 1.4.0 → 1.6.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/dist/index.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  const require_guards = require('./guards-DE5pQVvl.cjs');
2
2
  const require_number = require('./number-DRbo8lb6.cjs');
3
- const require_string = require('./string-CkqQ4Tys.cjs');
3
+ const require_string = require('./string-CxNDb3Ui.cjs');
4
4
 
5
5
  exports.capitalize = require_string.capitalize
6
6
  exports.clamp = require_number.clamp
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { isNotNull, isNotNullish, isNotUndefined, isTruthy } from "./guards.d-DXUlpL_S.cjs";
2
2
  import { clamp } from "./number.d-C1FAMQlq.cjs";
3
- import { capitalize, dedent, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string.d-DbrXP95T.cjs";
3
+ import { capitalize, dedent, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string.d-Dm-Z_ZP2.cjs";
4
4
  import { Arrayable, Awaitable, ElementOf, InferArguments, Nullable, Nullish } from "./types.d-fcYBBT6c.cjs";
5
5
  export { Arrayable, Awaitable, ElementOf, InferArguments, Nullable, Nullish, capitalize, clamp, dedent, isNotNull, isNotNullish, isNotUndefined, isTruthy, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { isNotNull$1 as isNotNull, isNotNullish$1 as isNotNullish, isNotUndefined$1 as isNotUndefined, isTruthy$1 as isTruthy } from "./guards.d-n1BzCANy.js";
2
2
  import { clamp$1 as clamp } from "./number.d-C2Xuq3Is.js";
3
- import { capitalize$1 as capitalize, dedent$1 as dedent, toCamelCase$1 as toCamelCase, toKebabCase$1 as toKebabCase, toPascalCase$1 as toPascalCase, toSnakeCase$1 as toSnakeCase } from "./string.d-Dv6EVJz4.js";
3
+ import { capitalize$1 as capitalize, dedent$1 as dedent, toCamelCase$1 as toCamelCase, toKebabCase$1 as toKebabCase, toPascalCase$1 as toPascalCase, toSnakeCase$1 as toSnakeCase } from "./string.d-BSY3kSBV.js";
4
4
  import { Arrayable, Awaitable, ElementOf, InferArguments, Nullable, Nullish } from "./types.d-BcKIY6l3.js";
5
5
  export { Arrayable, Awaitable, ElementOf, InferArguments, Nullable, Nullish, capitalize, clamp, dedent, isNotNull, isNotNullish, isNotUndefined, isTruthy, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { isNotNull, isNotNullish, isNotUndefined, isTruthy } from "./guards-O1HGJraI.js";
2
2
  import { clamp } from "./number-Bfr1z0Nr.js";
3
- import { capitalize, dedent, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string-C8IOUSBw.js";
3
+ import { capitalize, dedent, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string-bY1lSRQs.js";
4
4
 
5
5
  export { capitalize, clamp, dedent, isNotNull, isNotNullish, isNotUndefined, isTruthy, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
@@ -96,10 +96,18 @@ function toSnakeCase(str) {
96
96
  return str.trim().replace(/-/g, "_").replace(/\s+/g, " ").replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2").replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/\s+/g, "_").replace(/_+/g, "_").toLowerCase();
97
97
  }
98
98
  function dedent(strings, ...values) {
99
- const raw = typeof strings === "string" ? [strings] : strings.raw;
99
+ return internal_dedent(strings, values, false);
100
+ }
101
+ dedent.raw = dedentRaw;
102
+ function dedentRaw(strings, ...values) {
103
+ return internal_dedent(strings, values, true);
104
+ }
105
+ /** @internal */
106
+ function internal_dedent(strings, values, raw = false) {
107
+ const _raw = typeof strings === "string" ? [strings] : raw ? strings.raw : strings;
100
108
  let result = "";
101
- for (let i = 0; i < raw.length; i++) {
102
- const next = raw[i];
109
+ for (let i = 0; i < _raw.length; i++) {
110
+ const next = _raw[i];
103
111
  result += next;
104
112
  if (i < values.length) result += values[i];
105
113
  }
@@ -114,6 +122,22 @@ function dedent(strings, ...values) {
114
122
  const lastNonWhitespaceLine = whitespaceLines.lastIndexOf(false);
115
123
  return lines.slice(firstNonWhitespaceLine >= 0 ? firstNonWhitespaceLine : 0, lastNonWhitespaceLine >= 0 ? lastNonWhitespaceLine + 1 : lines.length).map((line) => line.slice(commonIndent)).join("\n");
116
124
  }
125
+ /**
126
+ * Ensures a string is a valid JavaScript identifier by prefixing with an underscore if necessary
127
+ * @param {string} str - The string to sanitize
128
+ * @returns {string} A valid JavaScript identifier
129
+ * @example
130
+ * ```ts
131
+ * sanitizeIdentifier("validName") // "validName"
132
+ * sanitizeIdentifier("123invalid") // "_123invalid"
133
+ * sanitizeIdentifier("$valid") // "$valid"
134
+ * sanitizeIdentifier("_valid") // "_valid"
135
+ * ```
136
+ */
137
+ function sanitizeIdentifier(str) {
138
+ const cleaned = str.replace(/[^\w$]/g, "");
139
+ return /^[A-Z_$]/i.test(cleaned) ? cleaned : `_${cleaned}`;
140
+ }
117
141
 
118
142
  //#endregion
119
143
  Object.defineProperty(exports, 'capitalize', {
@@ -128,6 +152,18 @@ Object.defineProperty(exports, 'dedent', {
128
152
  return dedent;
129
153
  }
130
154
  });
155
+ Object.defineProperty(exports, 'dedentRaw', {
156
+ enumerable: true,
157
+ get: function () {
158
+ return dedentRaw;
159
+ }
160
+ });
161
+ Object.defineProperty(exports, 'sanitizeIdentifier', {
162
+ enumerable: true,
163
+ get: function () {
164
+ return sanitizeIdentifier;
165
+ }
166
+ });
131
167
  Object.defineProperty(exports, 'toCamelCase', {
132
168
  enumerable: true,
133
169
  get: function () {
@@ -94,10 +94,18 @@ function toSnakeCase(str) {
94
94
  return str.trim().replace(/-/g, "_").replace(/\s+/g, " ").replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2").replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/\s+/g, "_").replace(/_+/g, "_").toLowerCase();
95
95
  }
96
96
  function dedent(strings, ...values) {
97
- const raw = typeof strings === "string" ? [strings] : strings.raw;
97
+ return internal_dedent(strings, values, false);
98
+ }
99
+ dedent.raw = dedentRaw;
100
+ function dedentRaw(strings, ...values) {
101
+ return internal_dedent(strings, values, true);
102
+ }
103
+ /** @internal */
104
+ function internal_dedent(strings, values, raw = false) {
105
+ const _raw = typeof strings === "string" ? [strings] : raw ? strings.raw : strings;
98
106
  let result = "";
99
- for (let i = 0; i < raw.length; i++) {
100
- const next = raw[i];
107
+ for (let i = 0; i < _raw.length; i++) {
108
+ const next = _raw[i];
101
109
  result += next;
102
110
  if (i < values.length) result += values[i];
103
111
  }
@@ -112,6 +120,22 @@ function dedent(strings, ...values) {
112
120
  const lastNonWhitespaceLine = whitespaceLines.lastIndexOf(false);
113
121
  return lines.slice(firstNonWhitespaceLine >= 0 ? firstNonWhitespaceLine : 0, lastNonWhitespaceLine >= 0 ? lastNonWhitespaceLine + 1 : lines.length).map((line) => line.slice(commonIndent)).join("\n");
114
122
  }
123
+ /**
124
+ * Ensures a string is a valid JavaScript identifier by prefixing with an underscore if necessary
125
+ * @param {string} str - The string to sanitize
126
+ * @returns {string} A valid JavaScript identifier
127
+ * @example
128
+ * ```ts
129
+ * sanitizeIdentifier("validName") // "validName"
130
+ * sanitizeIdentifier("123invalid") // "_123invalid"
131
+ * sanitizeIdentifier("$valid") // "$valid"
132
+ * sanitizeIdentifier("_valid") // "_valid"
133
+ * ```
134
+ */
135
+ function sanitizeIdentifier(str) {
136
+ const cleaned = str.replace(/[^\w$]/g, "");
137
+ return /^[A-Z_$]/i.test(cleaned) ? cleaned : `_${cleaned}`;
138
+ }
115
139
 
116
140
  //#endregion
117
- export { capitalize, dedent, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
141
+ export { capitalize, dedent, dedentRaw, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
package/dist/string.cjs CHANGED
@@ -1,7 +1,9 @@
1
- const require_string = require('./string-CkqQ4Tys.cjs');
1
+ const require_string = require('./string-CxNDb3Ui.cjs');
2
2
 
3
3
  exports.capitalize = require_string.capitalize
4
4
  exports.dedent = require_string.dedent
5
+ exports.dedentRaw = require_string.dedentRaw
6
+ exports.sanitizeIdentifier = require_string.sanitizeIdentifier
5
7
  exports.toCamelCase = require_string.toCamelCase
6
8
  exports.toKebabCase = require_string.toKebabCase
7
9
  exports.toPascalCase = require_string.toPascalCase
@@ -76,15 +76,44 @@ declare function toSnakeCase(str: string): string;
76
76
  * @param {TemplateStringsArray | string} literals - The string to dedent
77
77
  * @returns {string} The dedented string
78
78
  * @example ```ts
79
- dedent`
80
- This is a test.
81
- This is another line.
82
- `
83
- // "This is a test.\nThis is another line."
84
- ```
79
+ * dedent`
80
+ * This is a test.
81
+ * This is another line.
82
+ * `
83
+ * // "This is a test.\nThis is another line."
84
+ * ```
85
85
  */
86
86
  declare function dedent(literals: string): string;
87
87
  declare function dedent(strings: TemplateStringsArray, ...values: unknown[]): string;
88
+ declare namespace dedent {
89
+ var raw: typeof dedentRaw;
90
+ } /**
91
+ * Removes leading and trailing whitespace from each line of a string
92
+ * @param {TemplateStringsArray | string} literals - The string to dedent
93
+ * @returns {string} The dedented string
94
+ * @example ```ts
95
+ * dedent`
96
+ * This is a test.
97
+ * This is another line.
98
+ * `
99
+ * // "This is a test.\nThis is another line."
100
+ * ```
101
+ */
102
+
103
+ declare function dedentRaw(literals: string): string;
104
+ declare function dedentRaw(strings: TemplateStringsArray, ...values: unknown[]): string; /**
105
+ * Ensures a string is a valid JavaScript identifier by prefixing with an underscore if necessary
106
+ * @param {string} str - The string to sanitize
107
+ * @returns {string} A valid JavaScript identifier
108
+ * @example
109
+ * ```ts
110
+ * sanitizeIdentifier("validName") // "validName"
111
+ * sanitizeIdentifier("123invalid") // "_123invalid"
112
+ * sanitizeIdentifier("$valid") // "$valid"
113
+ * sanitizeIdentifier("_valid") // "_valid"
114
+ * ```
115
+ */
88
116
 
117
+ declare function sanitizeIdentifier(str: string): string;
89
118
  //#endregion
90
- export { capitalize as capitalize$1, dedent as dedent$1, toCamelCase as toCamelCase$1, toKebabCase as toKebabCase$1, toPascalCase as toPascalCase$1, toSnakeCase as toSnakeCase$1 };
119
+ export { capitalize as capitalize$1, dedent as dedent$1, dedentRaw as dedentRaw$1, sanitizeIdentifier as sanitizeIdentifier$1, toCamelCase as toCamelCase$1, toKebabCase as toKebabCase$1, toPascalCase as toPascalCase$1, toSnakeCase as toSnakeCase$1 };
@@ -76,15 +76,44 @@ declare function toSnakeCase(str: string): string;
76
76
  * @param {TemplateStringsArray | string} literals - The string to dedent
77
77
  * @returns {string} The dedented string
78
78
  * @example ```ts
79
- dedent`
80
- This is a test.
81
- This is another line.
82
- `
83
- // "This is a test.\nThis is another line."
84
- ```
79
+ * dedent`
80
+ * This is a test.
81
+ * This is another line.
82
+ * `
83
+ * // "This is a test.\nThis is another line."
84
+ * ```
85
85
  */
86
86
  declare function dedent(literals: string): string;
87
87
  declare function dedent(strings: TemplateStringsArray, ...values: unknown[]): string;
88
+ declare namespace dedent {
89
+ var raw: typeof dedentRaw;
90
+ } /**
91
+ * Removes leading and trailing whitespace from each line of a string
92
+ * @param {TemplateStringsArray | string} literals - The string to dedent
93
+ * @returns {string} The dedented string
94
+ * @example ```ts
95
+ * dedent`
96
+ * This is a test.
97
+ * This is another line.
98
+ * `
99
+ * // "This is a test.\nThis is another line."
100
+ * ```
101
+ */
102
+
103
+ declare function dedentRaw(literals: string): string;
104
+ declare function dedentRaw(strings: TemplateStringsArray, ...values: unknown[]): string; /**
105
+ * Ensures a string is a valid JavaScript identifier by prefixing with an underscore if necessary
106
+ * @param {string} str - The string to sanitize
107
+ * @returns {string} A valid JavaScript identifier
108
+ * @example
109
+ * ```ts
110
+ * sanitizeIdentifier("validName") // "validName"
111
+ * sanitizeIdentifier("123invalid") // "_123invalid"
112
+ * sanitizeIdentifier("$valid") // "$valid"
113
+ * sanitizeIdentifier("_valid") // "_valid"
114
+ * ```
115
+ */
88
116
 
117
+ declare function sanitizeIdentifier(str: string): string;
89
118
  //#endregion
90
- export { capitalize, dedent, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
119
+ export { capitalize, dedent, dedentRaw, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
package/dist/string.d.cts CHANGED
@@ -1,2 +1,2 @@
1
- import { capitalize, dedent, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string.d-DbrXP95T.cjs";
2
- export { capitalize, dedent, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
1
+ import { capitalize, dedent, dedentRaw, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string.d-Dm-Z_ZP2.cjs";
2
+ export { capitalize, dedent, dedentRaw, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
package/dist/string.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { capitalize$1 as capitalize, dedent$1 as dedent, toCamelCase$1 as toCamelCase, toKebabCase$1 as toKebabCase, toPascalCase$1 as toPascalCase, toSnakeCase$1 as toSnakeCase } from "./string.d-Dv6EVJz4.js";
2
- export { capitalize, dedent, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
1
+ import { capitalize$1 as capitalize, dedent$1 as dedent, dedentRaw$1 as dedentRaw, sanitizeIdentifier$1 as sanitizeIdentifier, toCamelCase$1 as toCamelCase, toKebabCase$1 as toKebabCase, toPascalCase$1 as toPascalCase, toSnakeCase$1 as toSnakeCase } from "./string.d-BSY3kSBV.js";
2
+ export { capitalize, dedent, dedentRaw, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
package/dist/string.js CHANGED
@@ -1,3 +1,3 @@
1
- import { capitalize, dedent, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string-C8IOUSBw.js";
1
+ import { capitalize, dedent, dedentRaw, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string-bY1lSRQs.js";
2
2
 
3
- export { capitalize, dedent, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
3
+ export { capitalize, dedent, dedentRaw, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luxass/utils",
3
- "version": "1.4.0",
3
+ "version": "1.6.0",
4
4
  "description": "A collection of utilities for JavaScript/TypeScript",
5
5
  "type": "module",
6
6
  "author": {
@@ -76,6 +76,7 @@
76
76
  "devDependencies": {
77
77
  "@luxass/eslint-config": "^4.18.1",
78
78
  "@types/node": "^22.15.2",
79
+ "@vitest/coverage-v8": "3.1.2",
79
80
  "eslint": "^9.25.1",
80
81
  "eslint-plugin-format": "^1.0.1",
81
82
  "publint": "^0.3.12",