Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@prosopo/util

Package Overview
Dependencies
Maintainers
3
Versions
69
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@prosopo/util - npm Package Compare versions

Comparing version 0.2.29 to 0.2.32

1

dist/isMain.d.ts

@@ -1,3 +0,2 @@

/// <reference types="node" resolution-mode="require"/>
export declare const isMain: (moduleOrImportMetaUrl: NodeModule | string, binName?: string) => boolean | "" | undefined;
//# sourceMappingURL=isMain.d.ts.map

16

dist/tests/util.test.js

@@ -81,2 +81,14 @@ import { at, get, merge, permutations } from '../util.js';

const v7 = at([1, 2, 3], 0, { optional: false });
const a3 = at([1, 2, 3], 0);
const a4 = at([1, 2, 3, undefined], 0);
const a6 = at([1, 2, 3], 0, { optional: true });
const a7 = at([1, 2, 3], 0, { optional: false });
const a8 = at([1, 2, 3], 0, {});
const a9 = at([1, 2, 3], 0, { noWrap: true });
const a5 = at('abc', 0);
const a10 = at('abc', 0, { optional: false });
const a11 = at('abc', 0, { optional: true });
const a12 = at([undefined, undefined, undefined], 0);
const a13 = at([undefined, undefined, undefined], 0, { optional: true });
const a14 = at([undefined, undefined, undefined], 0, { optional: false });
});

@@ -132,6 +144,2 @@ test('compatible with string', () => {

});
test('allow undefined out of bounds', () => {
expect(at([undefined, undefined, undefined], 3, { optional: true, noBoundsCheck: true })).to.equal(undefined);
expect(at([undefined, undefined, undefined], -1, { optional: true, noBoundsCheck: true })).to.equal(undefined);
});
});

@@ -138,0 +146,0 @@ describe('get', () => {

@@ -5,24 +5,21 @@ export declare const sleep: (ms: number) => Promise<unknown>;

}): Generator<number[]>;
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<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 type AtOptions = {
optional?: boolean;
noBoundsCheck?: boolean;
noWrap?: boolean;
};
export declare function at(str: string, i: number, options: {
export declare function at(str: string, index: number, options: AtOptions & {
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<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?: {
withReplacement?: boolean;
}) => {
choices: T[];
indices: number[];
};
export declare function at(str: string, index: number, options?: AtOptions): string;
export declare function at<T>(items: T[] | string, index: number, options: AtOptions & {
optional: false;
}): T;
export declare function at<T>(items: (T | undefined)[] | string, index: number, options: AtOptions & {
optional: true;
}): T | undefined;
export declare function at<T>(items: T[], index: number, options?: AtOptions): T;
export declare function getCurrentFileDirectory(url: string): string;

@@ -29,0 +26,0 @@ export declare const flattenObj: (obj: object, prefix?: string) => Record<string, unknown>;

@@ -25,49 +25,45 @@ export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

}
export function at(arr, i, options) {
export function get(obj, key, required = true) {
const value = obj[key];
if (required && value === undefined) {
throw new Error(`Object has no property '${String(key)}': ${JSON.stringify(obj, null, 2)}`);
}
return value;
}
export function at(items, index, options) {
if (items.length === 0) {
throw new Error('Array is empty');
}
if (!options?.noWrap) {
if (arr.length !== 0) {
i %= arr.length;
if (index > 0) {
index = index % items.length;
}
if (i < 0) {
i += arr.length;
else {
index = Math.ceil(Math.abs(index) / items.length) * items.length + index;
}
}
if (!options?.noBoundsCheck) {
if (i >= arr.length || i < 0) {
throw new Error(`Array index ${i} is out of bounds for array of length ${arr.length}: ${JSON.stringify(arr, null, 2)}`);
}
if (index >= items.length) {
throw new Error(`Index ${index} larger than array length ${items.length}`);
}
const el = arr[i];
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)}`);
if (index < 0) {
throw new Error(`Index ${index} smaller than 0`);
}
return el;
return items[index];
}
export function get(obj, key, required = true) {
const value = obj[key];
if (required && value === undefined) {
throw new Error(`Object has no property '${String(key)}': ${JSON.stringify(obj, null, 2)}`);
}
return value;
}
export const choice = (items, n, random, options) => {
function choice(items, n, random, options) {
if (n > items.length) {
throw new Error(`n (${n}) cannot be greater than items.length (${items.length})`);
throw new Error(`Cannot choose ${n} items from array of length ${items.length}`);
}
options = options || {};
const indicesSet = new Set();
const result = [];
const indices = [];
const choices = [];
while (indices.length < n) {
const index = Math.abs(Math.round(random())) % items.length;
if (options.withReplacement || indicesSet.add(index)) {
indices.push(index);
choices.push(at(items, index, { optional: true }));
}
for (let i = 0; i < n; i++) {
let index;
do {
index = Math.floor(Math.abs(random()) * items.length) % items.length;
} while (options?.withReplacement === false && indices.includes(index));
indices.push(index);
result.push(items[index]);
}
return {
choices,
indices,
};
};
return result;
}
export function getCurrentFileDirectory(url) {

@@ -74,0 +70,0 @@ return new URL(url).pathname.split('/').slice(0, -1).join('/');

{
"name": "@prosopo/util",
"version": "0.2.29",
"version": "0.2.32",
"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

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc