@augment-vir/common 15.4.2 → 15.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/cjs/augments/boolean.js +20 -0
- package/dist/cjs/augments/function.js +0 -5
- package/dist/cjs/augments/object/enum.js +13 -1
- package/dist/cjs/augments/object/matches-object-shape.js +2 -2
- package/dist/cjs/index.js +1 -0
- package/dist/esm/augments/boolean.js +14 -0
- package/dist/esm/augments/function.js +1 -3
- package/dist/esm/augments/object/enum.js +11 -0
- package/dist/esm/augments/object/matches-object-shape.js +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/types/augments/boolean.d.ts +6 -0
- package/dist/types/augments/function.d.ts +0 -1
- package/dist/types/augments/object/enum.d.ts +3 -0
- package/dist/types/index.d.ts +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ifTruthy = exports.isFalsy = exports.isTruthy = void 0;
|
|
4
|
+
function isTruthy(input) {
|
|
5
|
+
return !!input;
|
|
6
|
+
}
|
|
7
|
+
exports.isTruthy = isTruthy;
|
|
8
|
+
function isFalsy(input) {
|
|
9
|
+
return !input;
|
|
10
|
+
}
|
|
11
|
+
exports.isFalsy = isFalsy;
|
|
12
|
+
function ifTruthy(checkThis, ifTruthyCallback, ifFalsyCallback) {
|
|
13
|
+
if (isTruthy(checkThis)) {
|
|
14
|
+
return ifTruthyCallback(checkThis);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
return ifFalsyCallback(checkThis);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.ifTruthy = ifTruthy;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.filterToEnumValues = exports.isEnumValue = exports.getEnumTypedValues = exports.getEnumTypedKeys = void 0;
|
|
3
|
+
exports.filterToEnumValues = exports.ensureEnum = exports.isEnumValue = exports.getEnumTypedValues = exports.getEnumTypedKeys = void 0;
|
|
4
4
|
const object_entries_1 = require("./object-entries");
|
|
5
5
|
function getEnumTypedKeys(input) {
|
|
6
6
|
// enum keys are always strings
|
|
@@ -12,10 +12,22 @@ function getEnumTypedValues(input) {
|
|
|
12
12
|
return keys.map((key) => input[key]);
|
|
13
13
|
}
|
|
14
14
|
exports.getEnumTypedValues = getEnumTypedValues;
|
|
15
|
+
/** Check if the given value is within the given enum. */
|
|
15
16
|
function isEnumValue(input, checkEnum) {
|
|
16
17
|
return getEnumTypedValues(checkEnum).includes(input);
|
|
17
18
|
}
|
|
18
19
|
exports.isEnumValue = isEnumValue;
|
|
20
|
+
/** Interpret a primitive as an enum value with type safety. */
|
|
21
|
+
function ensureEnum(value, checkEnum) {
|
|
22
|
+
if (isEnumValue(value, checkEnum)) {
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
const enumValues = getEnumTypedValues(checkEnum);
|
|
27
|
+
throw new Error(`Given value '${value}' does not match given enum. Possible enum values: ${enumValues.join(',')}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.ensureEnum = ensureEnum;
|
|
19
31
|
function filterToEnumValues(inputs, checkEnum, caseInsensitive = false) {
|
|
20
32
|
if (caseInsensitive) {
|
|
21
33
|
return inputs.reduce((accum, currentInput) => {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.assertMatchesObjectShape = exports.matchesObjectShape = void 0;
|
|
4
|
+
const boolean_1 = require("../boolean");
|
|
4
5
|
const error_1 = require("../error");
|
|
5
|
-
const function_1 = require("../function");
|
|
6
6
|
const object_1 = require("./object");
|
|
7
7
|
const object_entries_1 = require("./object-entries");
|
|
8
8
|
const typed_has_property_1 = require("./typed-has-property");
|
|
@@ -104,7 +104,7 @@ function compareInnerValue(testValue, matchValue, throwKeyError, allowExtraProps
|
|
|
104
104
|
return new Error(`entry at index "${index}" did not match expected shape: ${(0, error_1.extractErrorMessage)(error)}`);
|
|
105
105
|
}
|
|
106
106
|
})
|
|
107
|
-
.filter(
|
|
107
|
+
.filter(boolean_1.isTruthy);
|
|
108
108
|
if (errors.length === matchValue.length) {
|
|
109
109
|
throw new Error(`entry at index "${index}" did not match any of the possible types from "${matchValue.join(', ')}"`);
|
|
110
110
|
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./augments/ansi"), exports);
|
|
18
18
|
__exportStar(require("./augments/array"), exports);
|
|
19
19
|
__exportStar(require("./augments/async"), exports);
|
|
20
|
+
__exportStar(require("./augments/boolean"), exports);
|
|
20
21
|
__exportStar(require("./augments/common-number"), exports);
|
|
21
22
|
__exportStar(require("./augments/common-string"), exports);
|
|
22
23
|
__exportStar(require("./augments/environment"), exports);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function isTruthy(input) {
|
|
2
|
+
return !!input;
|
|
3
|
+
}
|
|
4
|
+
export function isFalsy(input) {
|
|
5
|
+
return !input;
|
|
6
|
+
}
|
|
7
|
+
export function ifTruthy(checkThis, ifTruthyCallback, ifFalsyCallback) {
|
|
8
|
+
if (isTruthy(checkThis)) {
|
|
9
|
+
return ifTruthyCallback(checkThis);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
return ifFalsyCallback(checkThis);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -7,9 +7,20 @@ export function getEnumTypedValues(input) {
|
|
|
7
7
|
const keys = getEnumTypedKeys(input);
|
|
8
8
|
return keys.map((key) => input[key]);
|
|
9
9
|
}
|
|
10
|
+
/** Check if the given value is within the given enum. */
|
|
10
11
|
export function isEnumValue(input, checkEnum) {
|
|
11
12
|
return getEnumTypedValues(checkEnum).includes(input);
|
|
12
13
|
}
|
|
14
|
+
/** Interpret a primitive as an enum value with type safety. */
|
|
15
|
+
export function ensureEnum(value, checkEnum) {
|
|
16
|
+
if (isEnumValue(value, checkEnum)) {
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
const enumValues = getEnumTypedValues(checkEnum);
|
|
21
|
+
throw new Error(`Given value '${value}' does not match given enum. Possible enum values: ${enumValues.join(',')}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
13
24
|
export function filterToEnumValues(inputs, checkEnum, caseInsensitive = false) {
|
|
14
25
|
if (caseInsensitive) {
|
|
15
26
|
return inputs.reduce((accum, currentInput) => {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { isTruthy } from '../boolean';
|
|
1
2
|
import { extractErrorMessage } from '../error';
|
|
2
|
-
import { isTruthy } from '../function';
|
|
3
3
|
import { isObject } from './object';
|
|
4
4
|
import { getObjectTypedKeys } from './object-entries';
|
|
5
5
|
import { typedHasProperty } from './typed-has-property';
|
package/dist/esm/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './augments/ansi';
|
|
2
2
|
export * from './augments/array';
|
|
3
3
|
export * from './augments/async';
|
|
4
|
+
export * from './augments/boolean';
|
|
4
5
|
export * from './augments/common-number';
|
|
5
6
|
export * from './augments/common-string';
|
|
6
7
|
export * from './augments/environment';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type FalsyTypes = undefined | null | false | 0 | '' | -0 | 0n;
|
|
2
|
+
export type Truthy<T> = Exclude<T, FalsyTypes>;
|
|
3
|
+
export type Falsy<T> = Extract<T, FalsyTypes>;
|
|
4
|
+
export declare function isTruthy<T>(input: T): input is Truthy<T>;
|
|
5
|
+
export declare function isFalsy<T>(input: T): input is Falsy<T>;
|
|
6
|
+
export declare function ifTruthy<const InputType, IfTruthyType, IfFalsyType>(checkThis: InputType, ifTruthyCallback: (truthyInput: Truthy<InputType>) => IfTruthyType, ifFalsyCallback: (truthyInput: Falsy<InputType>) => IfFalsyType): IfTruthyType | IfFalsyType;
|
|
@@ -23,4 +23,3 @@ export type NoInputsFunction<ReturnGeneric = any> = () => ReturnGeneric;
|
|
|
23
23
|
* TypedFunction<[string | undefined], number>; // (input1: string|undefined) => number
|
|
24
24
|
*/
|
|
25
25
|
export type TypedFunction<Arguments, Return> = Arguments extends readonly any[] ? number extends Arguments['length'] ? (...args: ArrayElement<Arguments>[]) => Return : (...args: Arguments) => Return : void extends Arguments ? () => Return : (arg: Arguments) => Return;
|
|
26
|
-
export declare function isTruthy<T>(input: T): input is Exclude<T, undefined | null | false | 0 | '' | -0 | 0n>;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export declare function getEnumTypedKeys<T extends object>(input: T): (keyof T)[];
|
|
2
2
|
export declare function getEnumTypedValues<T extends object>(input: T): T[keyof T][];
|
|
3
|
+
/** Check if the given value is within the given enum. */
|
|
3
4
|
export declare function isEnumValue<T extends object>(input: unknown, checkEnum: T): input is T[keyof T];
|
|
5
|
+
/** Interpret a primitive as an enum value with type safety. */
|
|
6
|
+
export declare function ensureEnum<const ValueType extends `${EnumType[keyof EnumType]}`, const EnumType extends object>(value: ValueType, checkEnum: EnumType): EnumType;
|
|
4
7
|
export declare function filterToEnumValues<T extends object>(inputs: ReadonlyArray<unknown>, checkEnum: T, caseInsensitive?: boolean): T[keyof T][];
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './augments/ansi';
|
|
2
2
|
export * from './augments/array';
|
|
3
3
|
export * from './augments/async';
|
|
4
|
+
export * from './augments/boolean';
|
|
4
5
|
export * from './augments/common-number';
|
|
5
6
|
export * from './augments/common-string';
|
|
6
7
|
export * from './augments/environment';
|