cafe-utility 10.11.0 → 10.13.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.
Files changed (3) hide show
  1. package/index.d.ts +7 -3
  2. package/index.js +43 -9
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -325,9 +325,11 @@ declare function tickPlaybook<T>(playbook: Playbook<T>): {
325
325
  progress: number;
326
326
  data: T;
327
327
  } | null;
328
- declare function getArgument(args: string[], key: string): string | null;
329
- declare function requireStringArgument(args: string[], key: string): string;
330
- declare function requireNumberArgument(args: string[], key: string): number;
328
+ declare function getArgument(args: string[], key: string, env?: Record<string, string>, envKey?: string): string | null;
329
+ declare function getNumberArgument(args: string[], key: string, env?: Record<string, string>, envKey?: string): number | null;
330
+ declare function getBooleanArgument(args: string[], key: string, env?: Record<string, string>, envKey?: string): boolean | null;
331
+ declare function requireStringArgument(args: string[], key: string, env?: Record<string, string>, envKey?: string): string;
332
+ declare function requireNumberArgument(args: string[], key: string, env?: Record<string, string>, envKey?: string): number;
331
333
  declare function bringToFrontInPlace<T>(array: T[], index: number): void;
332
334
  declare function bringToFront<T>(array: T[], index: number): T[];
333
335
  declare type Point = {
@@ -394,6 +396,8 @@ export declare const Arrays: {
394
396
  organiseWithLimits: typeof organiseWithLimits;
395
397
  tickPlaybook: typeof tickPlaybook;
396
398
  getArgument: typeof getArgument;
399
+ getBooleanArgument: typeof getBooleanArgument;
400
+ getNumberArgument: typeof getNumberArgument;
397
401
  requireStringArgument: typeof requireStringArgument;
398
402
  requireNumberArgument: typeof requireNumberArgument;
399
403
  bringToFront: typeof bringToFront;
package/index.js CHANGED
@@ -1461,7 +1461,7 @@ const dateUnits = [
1461
1461
  function makeDate(numberWithUnit) {
1462
1462
  const number = parseFloat(numberWithUnit)
1463
1463
  if (isNaN(number)) {
1464
- throw 'makeDate got NaN for input'
1464
+ throw Error('makeDate got NaN for input')
1465
1465
  }
1466
1466
  const unit = numberWithUnit.replace(/^-?[0-9.]+/, '').trim()
1467
1467
  const index = dateUnits.findIndex(value => value[0] === unit.toLowerCase())
@@ -1748,7 +1748,7 @@ function formatNumber(number, options) {
1748
1748
  function makeNumber(numberWithUnit) {
1749
1749
  const number = parseFloat(numberWithUnit)
1750
1750
  if (isNaN(number)) {
1751
- throw 'makeNumber got NaN for input'
1751
+ throw Error('makeNumber got NaN for input')
1752
1752
  }
1753
1753
  const unit = numberWithUnit.replace(/^-?[0-9.]+/, '').trim()
1754
1754
  const index = shortNumberUnits.findIndex(value => value.toLowerCase() === unit.toLowerCase())
@@ -2207,11 +2207,11 @@ function tickPlaybook(playbook) {
2207
2207
  }
2208
2208
  }
2209
2209
 
2210
- function getArgument(args, key) {
2210
+ function getArgument(args, key, env, envKey) {
2211
2211
  const index = args.findIndex(arg => arg.endsWith('-' + key) || arg.includes('-' + key + '='))
2212
2212
  const arg = args[index]
2213
2213
  if (!arg) {
2214
- return null
2214
+ return (env || {})[envKey || key || ''] || null
2215
2215
  }
2216
2216
  if (arg.includes('=')) {
2217
2217
  return arg.split('=')[1]
@@ -2220,19 +2220,51 @@ function getArgument(args, key) {
2220
2220
  if (next && !next.startsWith('-')) {
2221
2221
  return next
2222
2222
  }
2223
- return null
2223
+ return (env || {})[envKey || key || ''] || null
2224
+ }
2225
+
2226
+ function getNumberArgument(args, key, env, envKey) {
2227
+ const value = getArgument(args, key, env, envKey)
2228
+ if (!value) {
2229
+ return null
2230
+ }
2231
+ try {
2232
+ return makeNumber(value)
2233
+ } catch (_a) {
2234
+ throw new Error(`Invalid number argument ${key}: ${value}`)
2235
+ }
2236
+ }
2237
+
2238
+ function getBooleanArgument(args, key, env, envKey) {
2239
+ const isPresent = args.some(arg => arg.endsWith('-' + key))
2240
+ const value = getArgument(args, key, env, envKey)
2241
+ if (!value && isPresent) {
2242
+ return true
2243
+ }
2244
+ if (!value && !isPresent) {
2245
+ return null
2246
+ }
2247
+ const truthy = ['true', '1', 'yes', 'y', 'on']
2248
+ const falsy = ['false', '0', 'no', 'n', 'off']
2249
+ if (truthy.includes(value.toLowerCase())) {
2250
+ return true
2251
+ }
2252
+ if (falsy.includes(value.toLowerCase())) {
2253
+ return false
2254
+ }
2255
+ throw Error(`Invalid boolean argument ${key}: ${value}`)
2224
2256
  }
2225
2257
 
2226
- function requireStringArgument(args, key) {
2227
- const value = getArgument(args, key)
2258
+ function requireStringArgument(args, key, env, envKey) {
2259
+ const value = getArgument(args, key, env, envKey)
2228
2260
  if (!value) {
2229
2261
  throw new Error(`Missing argument ${key}`)
2230
2262
  }
2231
2263
  return value
2232
2264
  }
2233
2265
 
2234
- function requireNumberArgument(args, key) {
2235
- const value = requireStringArgument(args, key)
2266
+ function requireNumberArgument(args, key, env, envKey) {
2267
+ const value = requireStringArgument(args, key, env, envKey)
2236
2268
  try {
2237
2269
  return makeNumber(value)
2238
2270
  } catch (_a) {
@@ -2581,6 +2613,8 @@ exports.Arrays = {
2581
2613
  organiseWithLimits,
2582
2614
  tickPlaybook,
2583
2615
  getArgument,
2616
+ getBooleanArgument,
2617
+ getNumberArgument,
2584
2618
  requireStringArgument,
2585
2619
  requireNumberArgument,
2586
2620
  bringToFront,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "10.11.0",
3
+ "version": "10.13.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {