@applitools/utils
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -6,2 +6,11 @@ # Change Log | ||
## 1.1.0 - 2021/3/24 | ||
- add `general.jwtDecode` function | ||
- add `guard.isOneOf` function | ||
- add `types.isEmpty` function for arrays, object, and string | ||
- add `types.instanceOf` signature with ctor name as a second argument instead of ctor itself | ||
- fix issue with not strict guards | ||
- improve guard's error messages | ||
## 1.0.1 - 2021/1/27 | ||
@@ -8,0 +17,0 @@ |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toString = exports.toJSON = exports.sleep = exports.guid = exports.getEnvValue = void 0; | ||
exports.toString = exports.toJSON = exports.sleep = exports.jwtDecode = exports.guid = exports.getEnvValue = void 0; | ||
const types = require("./types"); | ||
@@ -26,2 +26,9 @@ function getEnvValue(name, type) { | ||
exports.guid = guid; | ||
function jwtDecode(token) { | ||
let payloadSeg = token.split('.')[1]; | ||
payloadSeg += new Array(5 - (payloadSeg.length % 4)).join('='); | ||
payloadSeg = payloadSeg.replace(/-/g, '+').replace(/_/g, '/'); | ||
return JSON.parse(Buffer.from(payloadSeg, 'base64').toString()); | ||
} | ||
exports.jwtDecode = jwtDecode; | ||
function sleep(ms) { | ||
@@ -28,0 +35,0 @@ if (types.isNumber(ms)) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.custom = exports.instanceOf = exports.isEnumValue = exports.isObject = exports.isArray = exports.isNumeric = exports.isAlpha = exports.isAlphanumeric = exports.isString = exports.isGreaterThenOrEqual = exports.isGreaterThen = exports.isLessThenOrEqual = exports.isLessThen = exports.isInteger = exports.isNumber = exports.isBoolean = exports.notNull = void 0; | ||
exports.custom = exports.instanceOf = exports.isOneOf = exports.isEnumValue = exports.isObject = exports.isArray = exports.isNumeric = exports.isAlpha = exports.isAlphanumeric = exports.isString = exports.isGreaterThenOrEqual = exports.isGreaterThen = exports.isLessThenOrEqual = exports.isLessThen = exports.isInteger = exports.isNumber = exports.isBoolean = exports.notNull = void 0; | ||
const types = require("./types"); | ||
function notNull(value, { name }) { | ||
if (types.isNull(value)) { | ||
throw new Error(`IllegalArgument: ${name} is null or undefined`); | ||
throw new Error(`IllegalArgument: ${name} is not allowed to be null or undefined`); | ||
} | ||
@@ -12,6 +12,4 @@ } | ||
function isBoolean(value, { name, strict = true }) { | ||
if (strict) | ||
notNull(value, { name }); | ||
if (!types.isBoolean(value)) { | ||
throw new Error(`IllegalType: ${name} is not a boolean`); | ||
if ((strict || !types.isNull(value)) && !types.isBoolean(value)) { | ||
throw new Error(`IllegalType: ${name} must be of type boolean. Received ${value}`); | ||
} | ||
@@ -21,6 +19,4 @@ } | ||
function isNumber(value, { name, strict = true, lt, lte, gt, gte }) { | ||
if (strict) | ||
notNull(value, { name }); | ||
if (!types.isNumber(value)) { | ||
throw new Error(`IllegalArgument: ${name} is not a number`); | ||
if ((strict || !types.isNull(value)) && !types.isNumber(value)) { | ||
throw new Error(`IllegalArgument: ${name} must be of type number. Received ${value}`); | ||
} | ||
@@ -38,6 +34,4 @@ if (!types.isNull(lt)) | ||
function isInteger(value, { name, strict = true, lt, lte, gt, gte }) { | ||
if (strict) | ||
notNull(value, { name }); | ||
if (!types.isInteger(value)) { | ||
throw new Error(`IllegalArgument: ${name} is not an integer`); | ||
if ((strict || !types.isNull(value)) && !types.isInteger(value)) { | ||
throw new Error(`IllegalArgument: ${name} must be an integer of type number. Received ${value}`); | ||
} | ||
@@ -56,3 +50,3 @@ if (!types.isNull(lt)) | ||
if (!(value < limit)) { | ||
throw new Error(`IllegalArgument: ${name} must be < ${limit}`); | ||
throw new Error(`IllegalArgument: ${name} must be < ${limit}. Received ${value}`); | ||
} | ||
@@ -63,3 +57,3 @@ } | ||
if (!(value <= limit)) { | ||
throw new Error(`IllegalArgument: ${name} must be <= ${limit}`); | ||
throw new Error(`IllegalArgument: ${name} must be <= ${limit}. Received ${value}`); | ||
} | ||
@@ -70,3 +64,3 @@ } | ||
if (!(value > limit)) { | ||
throw new Error(`IllegalArgument: ${name} must be > ${limit}`); | ||
throw new Error(`IllegalArgument: ${name} must be > ${limit}. Received ${value}`); | ||
} | ||
@@ -77,3 +71,3 @@ } | ||
if (!(value >= limit)) { | ||
throw new Error(`IllegalArgument: ${name} must be >= ${limit}`); | ||
throw new Error(`IllegalArgument: ${name} must be >= ${limit}. Received ${value}`); | ||
} | ||
@@ -83,6 +77,4 @@ } | ||
function isString(value, { name, strict = true, alpha, numeric }) { | ||
if (strict) | ||
notNull(value, { name }); | ||
if (!types.isString(value)) { | ||
throw new Error(`IllegalArgument: ${name} is not a string`); | ||
if ((strict || !types.isNull(value)) && !types.isString(value)) { | ||
throw new Error(`IllegalArgument: ${name} must be of type string. Received ${value}`); | ||
} | ||
@@ -99,3 +91,3 @@ if (alpha && numeric) | ||
if (!/^[a-z0-9]+$/i.test(value)) { | ||
throw new Error(`IllegalArgument: ${name} is not alphanumeric`); | ||
throw new Error(`IllegalArgument: ${name} must be an alphanumeric string. Received ${value}`); | ||
} | ||
@@ -106,3 +98,3 @@ } | ||
if (!/^[a-z]+$/i.test(value)) { | ||
throw new Error(`IllegalArgument: ${name} is not alphabetic`); | ||
throw new Error(`IllegalArgument: ${name} must be an alphabetic string. Received ${value}`); | ||
} | ||
@@ -113,3 +105,3 @@ } | ||
if (!/^[0-9]+$/.test(value)) { | ||
throw new Error(`IllegalArgument: ${name} is not numeric`); | ||
throw new Error(`IllegalArgument: ${name} must be a numeric sring. Received ${value}`); | ||
} | ||
@@ -119,6 +111,4 @@ } | ||
function isArray(value, { name, strict = true }) { | ||
if (strict) | ||
notNull(value, { name }); | ||
if (!types.isArray(value)) { | ||
throw new Error(`IllegalArgument: ${name} is not an array`); | ||
if ((strict || !types.isNull(value)) && !types.isArray(value)) { | ||
throw new Error(`IllegalArgument: ${name} must be of type array. Received ${value}`); | ||
} | ||
@@ -128,6 +118,4 @@ } | ||
function isObject(value, { name, strict = true }) { | ||
if (strict) | ||
notNull(value, { name }); | ||
if (!types.isObject(value)) { | ||
throw new Error(`IllegalArgument: ${name} is not an object`); | ||
if ((strict || !types.isNull(value)) && !types.isObject(value)) { | ||
throw new Error(`IllegalArgument: ${name} must be of type object. Received ${value}`); | ||
} | ||
@@ -137,15 +125,19 @@ } | ||
function isEnumValue(value, enumeration, { name, strict = true }) { | ||
if (strict) | ||
notNull(value, { name }); | ||
const values = new Set(Object.values(enumeration)); | ||
if (!values.has(value)) { | ||
throw new Error(`IllegalArgument: ${name} should be one of [${Array.from(values, value => JSON.stringify(value)).join(', ')}]`); | ||
if ((strict || !types.isNull(value)) && !values.has(value)) { | ||
const list = Array.from(values, value => JSON.stringify(value)).join(', '); | ||
throw new Error(`IllegalArgument: ${name} must be one of [${list}]. Received ${value}`); | ||
} | ||
} | ||
exports.isEnumValue = isEnumValue; | ||
function isOneOf(value, values, { name, strict = true }) { | ||
if ((strict || !types.isNull(value)) && !values.includes(value)) { | ||
const list = values.map(value => JSON.stringify(value)).join(', '); | ||
throw new Error(`IllegalArgument: ${name} must be one of [${list}]. Received ${value}`); | ||
} | ||
} | ||
exports.isOneOf = isOneOf; | ||
function instanceOf(value, ctor, { name, strict = true }) { | ||
if (strict) | ||
notNull(value, { name }); | ||
if (!types.instanceOf(value, ctor)) { | ||
throw new Error(`IllegalType: ${name} is not an instance of ${ctor.name}`); | ||
if ((strict || !types.isNull(value)) && !types.instanceOf(value, ctor)) { | ||
throw new Error(`IllegalType: ${name} must be an instance of ${ctor.name}`); | ||
} | ||
@@ -155,6 +147,4 @@ } | ||
function custom(value, check, { name, strict = true, message }) { | ||
if (strict) | ||
notNull(value, { name }); | ||
if (!check(value)) { | ||
throw new Error(`IllegalType: ${name} ${message || 'is unknown type'}`); | ||
if ((strict || !types.isNull(value)) && !check(value)) { | ||
throw new Error(`IllegalType: ${name} ${message || 'is wrong type'}`); | ||
} | ||
@@ -161,0 +151,0 @@ } |
"use strict"; | ||
/* eslint {"@typescript-eslint/ban-types": ["error", {"types": {"Function": false}}]} */ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.instanceOf = exports.has = exports.isEnumValue = exports.isFunction = exports.isObject = exports.isArray = exports.isInteger = exports.isNumber = exports.isBase64 = exports.isString = exports.isBoolean = exports.isNull = void 0; | ||
exports.instanceOf = exports.has = exports.isEnumValue = exports.isFunction = exports.isEmpty = exports.isObject = exports.isArray = exports.isInteger = exports.isNumber = exports.isBase64 = exports.isString = exports.isBoolean = exports.isNull = void 0; | ||
function isNull(value) { | ||
@@ -36,3 +37,13 @@ return value == null; | ||
exports.isObject = isObject; | ||
function isFunction(value) { | ||
function isEmpty(value) { | ||
if (!value) | ||
return true; | ||
if (isObject(value)) | ||
return Object.keys(value).length === 0; | ||
return value.length === 0; | ||
} | ||
exports.isEmpty = isEmpty; | ||
function isFunction(value, key) { | ||
if (key && has(value, key)) | ||
return typeof value[key] === 'function'; | ||
return typeof value === 'function'; | ||
@@ -59,7 +70,14 @@ } | ||
exports.has = has; | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
function instanceOf(value, ctor) { | ||
return value instanceof ctor; | ||
function instanceOf(value, ctorOrName) { | ||
if (!isString(ctorOrName)) | ||
return value instanceof ctorOrName; | ||
let proto = Object.getPrototypeOf(value); | ||
while (proto) { | ||
if (proto.constructor.name === ctorOrName) | ||
return true; | ||
proto = Object.getPrototypeOf(proto); | ||
} | ||
return false; | ||
} | ||
exports.instanceOf = instanceOf; | ||
//# sourceMappingURL=types.js.map |
{ | ||
"name": "@applitools/utils", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"keywords": [ | ||
@@ -27,5 +27,9 @@ "applitools", | ||
"directories": { | ||
"src": "./lib", | ||
"src": "./src", | ||
"test": "./test" | ||
}, | ||
"files": [ | ||
"dist", | ||
"types" | ||
], | ||
"main": "./dist/index.js", | ||
@@ -32,0 +36,0 @@ "types": "./types/index.d.ts", |
export declare function getEnvValue<T extends 'boolean' | 'number' | 'string' = 'string'>(name: string, type?: T): T extends 'boolean' ? boolean : T extends 'number' ? number : string; | ||
export declare function guid(): string; | ||
export declare function jwtDecode(token: string): Record<string, any>; | ||
export declare function sleep(ms: number): Promise<unknown>; | ||
@@ -4,0 +5,0 @@ export declare function toJSON<TObject extends Record<PropertyKey, any>, TKey extends string, TProps extends Readonly<TKey[]>>(object: TObject, props: TProps): { |
@@ -21,3 +21,3 @@ declare type NamedParam = { | ||
export declare function notNull(value: any, { name }: NamedParam): void; | ||
export declare function isBoolean(value: boolean, { name, strict }: StrictParam): void; | ||
export declare function isBoolean(value: any, { name, strict }: StrictParam): void; | ||
export declare function isNumber(value: any, { name, strict, lt, lte, gt, gte }: NumberParam): void; | ||
@@ -36,2 +36,3 @@ export declare function isInteger(value: any, { name, strict, lt, lte, gt, gte }: NumberParam): void; | ||
export declare function isEnumValue(value: any, enumeration: Record<string, any>, { name, strict }: StrictParam): void; | ||
export declare function isOneOf<TValue>(value: any, values: readonly TValue[], { name, strict }: StrictParam): void; | ||
export declare function instanceOf(value: any, ctor: new (...args: any) => any, { name, strict }: StrictParam): void; | ||
@@ -38,0 +39,0 @@ export declare function custom(value: any, check: (value: any) => boolean, { name, strict, message }: CustomParam): void; |
@@ -9,6 +9,13 @@ export declare function isNull(value: any): value is null | undefined; | ||
export declare function isObject(value: any): value is Record<PropertyKey, any>; | ||
export declare function isEmpty(value: Record<PropertyKey, unknown>): value is Record<PropertyKey, never>; | ||
export declare function isEmpty(value: any[]): value is []; | ||
export declare function isEmpty(value: string): value is ''; | ||
export declare function isFunction(value: any): value is (...args: any[]) => any; | ||
export declare function isFunction<TKey extends PropertyKey>(value: any, key: TKey): value is { | ||
[key in TKey]: (...args: any[]) => any; | ||
}; | ||
export declare function isEnumValue<TEnum extends Record<string, string | number>, TValues extends TEnum[keyof TEnum]>(value: any, enumeration: TEnum): value is TValues; | ||
export declare function has<TKey extends PropertyKey>(value: any, keys: TKey | readonly TKey[]): value is Record<TKey, unknown>; | ||
export declare function instanceOf<TCtor>(value: any, ctorName: string): value is TCtor; | ||
export declare function instanceOf<TCtor extends Function>(value: any, ctor: TCtor): value is TCtor['prototype']; | ||
//# sourceMappingURL=types.d.ts.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
0
69289
27
588