cafe-utility 10.12.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.
- package/index.d.ts +4 -0
- package/index.js +36 -2
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -326,6 +326,8 @@ declare function tickPlaybook<T>(playbook: Playbook<T>): {
|
|
|
326
326
|
data: T;
|
|
327
327
|
} | null;
|
|
328
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;
|
|
329
331
|
declare function requireStringArgument(args: string[], key: string, env?: Record<string, string>, envKey?: string): string;
|
|
330
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;
|
|
@@ -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())
|
|
@@ -2223,6 +2223,38 @@ function getArgument(args, key, env, envKey) {
|
|
|
2223
2223
|
return (env || {})[envKey || key || ''] || null
|
|
2224
2224
|
}
|
|
2225
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}`)
|
|
2256
|
+
}
|
|
2257
|
+
|
|
2226
2258
|
function requireStringArgument(args, key, env, envKey) {
|
|
2227
2259
|
const value = getArgument(args, key, env, envKey)
|
|
2228
2260
|
if (!value) {
|
|
@@ -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,
|