@prosopo/util
Advanced tools
Comparing version 0.2.4 to 0.2.5
@@ -15,10 +15,18 @@ import _lodash from 'lodash'; | ||
}): Generator<number[]>; | ||
export declare const at: <T>(arr: T[], i: number, options?: { | ||
required?: boolean; | ||
checkBounds?: boolean; | ||
wrap?: boolean; | ||
}) => T; | ||
export type AtOptions = { | ||
optional?: boolean; | ||
noBoundsCheck?: boolean; | ||
noWrap?: boolean; | ||
}; | ||
export declare function at(str: string, i: number, options: { | ||
optional: true; | ||
noBoundsCheck?: boolean; | ||
noWrap?: boolean; | ||
}): string | undefined; | ||
export declare function at(str: string, i: number, options?: AtOptions): string; | ||
export declare function at<T>(arr: T[], i: number, options?: AtOptions): T; | ||
export declare function get<T>(obj: T, key: unknown, required?: true): Exclude<T[keyof T], undefined>; | ||
export declare function get<T>(obj: T, key: unknown, required: false): T[keyof T] | undefined; | ||
export declare function get<V>(obj: unknown, key: unknown, required?: true): V; | ||
export declare function get<T>(obj: unknown, key: string | number | symbol, required?: true): Exclude<T, undefined>; | ||
export declare function get<T>(obj: unknown, key: string | number | symbol, required: false): T | undefined; | ||
export declare const choice: <T>(items: T[], n: number, random: () => number, options?: { | ||
@@ -25,0 +33,0 @@ withReplacement?: boolean; |
@@ -84,9 +84,4 @@ import _lodash from 'lodash'; | ||
} | ||
// Get an element from an array, throwing an error if it's index is out of bounds or if the element is undefined or null | ||
// Note undefined's are not allowed due to arrays returning undefined when accessing an out of bounds index | ||
export const at = (arr, i, options = {}) => { | ||
options.checkBounds = options.checkBounds ?? true; | ||
options.required = options.required ?? true; | ||
options.wrap = options.wrap ?? true; | ||
if (options.wrap) { | ||
export function at(arr, i, options) { | ||
if (!options?.noWrap) { | ||
if (arr.length !== 0) { | ||
@@ -99,3 +94,3 @@ i %= arr.length; | ||
} | ||
if (options.checkBounds) { | ||
if (!options?.noBoundsCheck) { | ||
if (i >= arr.length || i < 0) { | ||
@@ -106,7 +101,7 @@ throw new Error(`Array index ${i} is out of bounds for array of length ${arr.length}: ${JSON.stringify(arr, null, 2)}`); | ||
const el = arr[i]; | ||
if (options.required && el === undefined) { | ||
if (!options?.optional && el === undefined) { | ||
throw new Error(`Array item at index ${i} is undefined for array of length ${arr.length}: ${JSON.stringify(arr, null, 2)}`); | ||
} | ||
return el; | ||
}; | ||
} | ||
export function get(obj, key, required = true) { | ||
@@ -133,3 +128,3 @@ const value = obj[key]; | ||
indices.push(index); | ||
choices.push(at(items, index, { required: false })); | ||
choices.push(at(items, index, { optional: true })); | ||
} | ||
@@ -136,0 +131,0 @@ } |
{ | ||
"name": "@prosopo/util", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"author": "PROSOPO LIMITED <info@prosopo.io>", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
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
649976