@gesslar/toolkit 2.0.0 → 2.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gesslar/toolkit",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "A collection of utilities for Node.js and browser environments.",
5
5
  "main": "./src/index.js",
6
6
  "type": "module",
package/src/lib/FS.js CHANGED
@@ -153,9 +153,8 @@ export default class FS {
153
153
  const to = path2.split(sep).filter(Boolean)
154
154
 
155
155
  // If they're the same, just return path1
156
- if(to.length === from.length && from.every((f, i) => to[i] === f)) {
156
+ if(to.length === from.length && from.every((f, i) => to[i] === f))
157
157
  return path1
158
- }
159
158
 
160
159
  const overlapIndex = from.findLastIndex(curr => curr === to.at(0))
161
160
 
package/src/lib/Util.js CHANGED
@@ -1,7 +1,10 @@
1
1
  import {createHash} from "node:crypto"
2
2
  import {EventEmitter} from "node:events"
3
- import Sass from "./Sass.js"
4
3
  import {Util as BrowserUtil} from "../browser/index.js"
4
+ import Sass from "./Sass.js"
5
+ import process from "node:process"
6
+ import JSON5 from "json5"
7
+ import Valid from "./Valid.js"
5
8
 
6
9
  /**
7
10
  * Utility class providing common helper functions for string manipulation,
@@ -152,4 +155,45 @@ export default class Util extends BrowserUtil {
152
155
  )
153
156
  }
154
157
  }
158
+
159
+ /**
160
+ * Retrieves an environment variable and parses it as JSON5.
161
+ *
162
+ * This method fetches the value of the specified environment variable and
163
+ * attempts to parse it using JSON5. If the variable doesn't exist or is
164
+ * empty, the default value is returned. If parsing fails, an error is
165
+ * thrown.
166
+ *
167
+ * Example:
168
+ * // export MY_CONFIG='{"debug": true, timeout: 5000}'
169
+ * Util.getEnv("MY_CONFIG", {debug: false})
170
+ * → {debug: true, timeout: 5000}
171
+ *
172
+ * Edge cases:
173
+ * - If the environment variable doesn't exist, returns the default value
174
+ * - If the value is an empty string, returns the default value
175
+ * - If JSON5 parsing fails, throws a Sass error with context
176
+ *
177
+ * @param {string} ev - Name of the environment variable to retrieve
178
+ * @param {unknown} [def=undefined] - Default value if variable doesn't exist or is empty
179
+ * @returns {unknown} Parsed JSON5 value or default
180
+ * @throws {Sass} If JSON5 parsing fails
181
+ */
182
+ static getEnv(ev, def=undefined) {
183
+ Valid.type(ev, "String")
184
+
185
+ const value = process.env[ev]
186
+
187
+ if(!value)
188
+ return def
189
+
190
+ try {
191
+ return JSON5.parse(value)
192
+ } catch(error) {
193
+ throw Sass.new(
194
+ `Failed to parse environment variable '${ev}' as JSON5`,
195
+ error
196
+ )
197
+ }
198
+ }
155
199
  }
@@ -1 +1 @@
1
- {"version":3,"file":"FS.d.ts","sourceRoot":"","sources":["../../lib/FS.js"],"names":[],"mappings":"AAwBA;;GAEG;AACH;IACE,kCAAwB;IACxB,uCAAkC;IAClC,mBAAsB;IAEtB;;;;;;OAMG;IACH,4BAHW,MAAM,GACJ,MAAM,CAIlB;IAED;;;;;;OAMG;IACH,2BAHW,MAAM,GACJ,MAAM,CAUlB;IAED;;;;;;OAMG;IACH,2BAHW,MAAM,GACJ,MAAM,CAQlB;IAED;;;;;;;;OAQG;IACH,sBALW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,GAClB,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAmCtC;IAED;;;;;;;;;;OAUG;IACH,oCAJW,UAAU,GAAC,eAAe,MAC1B,UAAU,GAAC,eAAe,GACxB,MAAM,CAYlB;IAED;;;;;;;;OAQG;IACH,oCALW,MAAM,SACN,MAAM,QACN,MAAM,GACJ,MAAM,CA2BlB;IAED;;;;;;;;OAQG;IACH,6BAJW,MAAM,UACN,MAAM,GACJ,MAAM,CAgClB;CACF;yBAzMa,OAAO,iBAAiB,EAAE,OAAO;8BACjC,OAAO,sBAAsB,EAAE,OAAO"}
1
+ {"version":3,"file":"FS.d.ts","sourceRoot":"","sources":["../../lib/FS.js"],"names":[],"mappings":"AAwBA;;GAEG;AACH;IACE,kCAAwB;IACxB,uCAAkC;IAClC,mBAAsB;IAEtB;;;;;;OAMG;IACH,4BAHW,MAAM,GACJ,MAAM,CAIlB;IAED;;;;;;OAMG;IACH,2BAHW,MAAM,GACJ,MAAM,CAUlB;IAED;;;;;;OAMG;IACH,2BAHW,MAAM,GACJ,MAAM,CAQlB;IAED;;;;;;;;OAQG;IACH,sBALW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,GAClB,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAmCtC;IAED;;;;;;;;;;OAUG;IACH,oCAJW,UAAU,GAAC,eAAe,MAC1B,UAAU,GAAC,eAAe,GACxB,MAAM,CAYlB;IAED;;;;;;;;OAQG;IACH,oCALW,MAAM,SACN,MAAM,QACN,MAAM,GACJ,MAAM,CA0BlB;IAED;;;;;;;;OAQG;IACH,6BAJW,MAAM,UACN,MAAM,GACJ,MAAM,CAgClB;CACF;yBAxMa,OAAO,iBAAiB,EAAE,OAAO;8BACjC,OAAO,sBAAsB,EAAE,OAAO"}
@@ -71,6 +71,30 @@ export default class Util extends BrowserUtil {
71
71
  * @returns {Promise<void>} Resolves when all listeners have completed, but no grapes.
72
72
  */
73
73
  static asyncEmitQuack(emitter: object, event: string, ...args: unknown[]): Promise<void>;
74
+ /**
75
+ * Retrieves an environment variable and parses it as JSON5.
76
+ *
77
+ * This method fetches the value of the specified environment variable and
78
+ * attempts to parse it using JSON5. If the variable doesn't exist or is
79
+ * empty, the default value is returned. If parsing fails, an error is
80
+ * thrown.
81
+ *
82
+ * Example:
83
+ * // export MY_CONFIG='{"debug": true, timeout: 5000}'
84
+ * Util.getEnv("MY_CONFIG", {debug: false})
85
+ * → {debug: true, timeout: 5000}
86
+ *
87
+ * Edge cases:
88
+ * - If the environment variable doesn't exist, returns the default value
89
+ * - If the value is an empty string, returns the default value
90
+ * - If JSON5 parsing fails, throws a Sass error with context
91
+ *
92
+ * @param {string} ev - Name of the environment variable to retrieve
93
+ * @param {unknown} [def=undefined] - Default value if variable doesn't exist or is empty
94
+ * @returns {unknown} Parsed JSON5 value or default
95
+ * @throws {Sass} If JSON5 parsing fails
96
+ */
97
+ static getEnv(ev: string, def?: unknown): unknown;
74
98
  }
75
99
  import { Util as BrowserUtil } from "../browser/index.js";
76
100
  //# sourceMappingURL=Util.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Util.d.ts","sourceRoot":"","sources":["../../lib/Util.js"],"names":[],"mappings":"AAKA;;;GAGG;AACH;IACE;;;;;OAKG;IACH,iBAHW,MAAM,GACJ,MAAM,CAIlB;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,mCAHW,MAAM,GACJ,KAAK,CAAC,MAAM,CAAC,CAazB;IAED;;;;;;;;OAQG;IACH,+CALW,MAAM,SACN,MAAM,WACH,OAAO,EAAA,GACR,OAAO,CAAC,IAAI,CAAC,CAmBzB;IAED;;;;;;;;;;;;OAYG;IACH,0BALW,YAAY,SACZ,MAAM,WACH,OAAO,EAAA,GACR,OAAO,CAAC,IAAI,CAAC,CAqBzB;IAED;;;;;;;;;;OAUG;IACH,+BALW,MAAM,SACN,MAAM,WACH,OAAO,EAAA,GACR,OAAO,CAAC,IAAI,CAAC,CAyBzB;CACF;oCAvJiC,qBAAqB"}
1
+ {"version":3,"file":"Util.d.ts","sourceRoot":"","sources":["../../lib/Util.js"],"names":[],"mappings":"AAQA;;;GAGG;AACH;IACE;;;;;OAKG;IACH,iBAHW,MAAM,GACJ,MAAM,CAIlB;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,mCAHW,MAAM,GACJ,KAAK,CAAC,MAAM,CAAC,CAazB;IAED;;;;;;;;OAQG;IACH,+CALW,MAAM,SACN,MAAM,WACH,OAAO,EAAA,GACR,OAAO,CAAC,IAAI,CAAC,CAmBzB;IAED;;;;;;;;;;;;OAYG;IACH,0BALW,YAAY,SACZ,MAAM,WACH,OAAO,EAAA,GACR,OAAO,CAAC,IAAI,CAAC,CAqBzB;IAED;;;;;;;;;;OAUG;IACH,+BALW,MAAM,SACN,MAAM,WACH,OAAO,EAAA,GACR,OAAO,CAAC,IAAI,CAAC,CAyBzB;IAED;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,kBALW,MAAM,QACN,OAAO,GACL,OAAO,CAmBnB;CACF;oCApMiC,qBAAqB"}