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

@bscotch/utility

Package Overview
Dependencies
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bscotch/utility - npm Package Compare versions

Comparing version 0.15.0 to 0.16.0

44

build/lib/array.d.ts

@@ -8,2 +8,4 @@ import { EmptyArray } from './types';

export declare function wrapIfNotArray<Item>(item: Item): Item extends any[] ? Item : Item extends undefined ? [] : [Item];
/** @alias wrapIfNotArray */
export declare const arrayTouch: typeof wrapIfNotArray;
/**

@@ -16,2 +18,4 @@ * Return `true` if the `comparison` function returns true

export declare function eachTruthyComparedToLast<ArrayOfComparables extends any[]>(arrayOfComparables: ArrayOfComparables, comparison: (currentValue: ArrayOfComparables[number], lastValue: ArrayOfComparables[number]) => boolean): boolean;
/** @alias eachTruthyComparedToLast */
export declare const arrayEachTruthyComparedToLast: typeof eachTruthyComparedToLast;
/**

@@ -21,2 +25,4 @@ * Return true if each value is greater than the last

export declare function valuesAreIncreasing<ArrayOfValues extends any[]>(increasingArray: ArrayOfValues): boolean;
/** @alias valuesAreIncreasing */
export declare const arrayValuesAreIncreasing: typeof valuesAreIncreasing;
/**

@@ -26,2 +32,4 @@ * Return true if each value is greater than the last

export declare function valuesAreDecreasing<ArrayOfValues extends any[]>(decreasingArray: ArrayOfValues): boolean;
/** @alias valuesAreDecreasing */
export declare const arrayValuesAreDecreasing: typeof valuesAreDecreasing;
declare type FirstItemArray<Item> = Item[] | [Item, ...any[]] | Readonly<[Item, ...any[]]> | EmptyArray;

@@ -32,3 +40,39 @@ /**

export declare function selfOrFirstItem<Item extends any>(items: FirstItemArray<Item> | Item): Item extends EmptyArray ? undefined : Item;
/** @alias selfOrFirstItem */
export declare const arrayUntouch: typeof selfOrFirstItem;
declare type SortReturn = number;
/**
* Ascending-sort an array of numeric values.
* Can call on an array, or pass to `Array.sort`
*
* ```js
* const array = [10,3,11];
* // Both ways work
* arraySortNumeric(array);
* array.sort(arraySortNumeric);
* ```
*/
export declare function arraySortNumeric<N extends number[]>(numbers: N): number[];
export declare function arraySortNumeric<N extends number>(array1Item: N, array2Item: N): SortReturn;
/**
* Descending-sort an array of numeric values.
* Can call on an array, or pass to `Array.sort`
*
* ```js
* const array = [10,3,11];
* // Both ways work
* arraySortNumericDescending(array);
* array.sort(arraySortNumericDescending);
* ```
*/
export declare function arraySortNumericDescending<N extends number[]>(numbers: N): number[];
export declare function arraySortNumericDescending<N extends number>(array1Item: N, array2Item: N): SortReturn;
export declare const array: {
arrayTouch: typeof wrapIfNotArray;
arrayEachTruthyComparedToLast: typeof eachTruthyComparedToLast;
arrayUntouch: typeof selfOrFirstItem;
arrayValuesAreDecreasing: typeof valuesAreDecreasing;
arrayValuesAreIncreasing: typeof valuesAreIncreasing;
arraySortNumeric: typeof arraySortNumeric;
arraySortNumericDescending: typeof arraySortNumericDescending;
eachTruthyComparedToLast: typeof eachTruthyComparedToLast;

@@ -35,0 +79,0 @@ selfOrFirstItem: typeof selfOrFirstItem;

44

build/lib/array.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.array = exports.selfOrFirstItem = exports.valuesAreDecreasing = exports.valuesAreIncreasing = exports.eachTruthyComparedToLast = exports.wrapIfNotArray = void 0;
exports.array = exports.arraySortNumericDescending = exports.arraySortNumeric = exports.arrayUntouch = exports.selfOrFirstItem = exports.arrayValuesAreDecreasing = exports.valuesAreDecreasing = exports.arrayValuesAreIncreasing = exports.valuesAreIncreasing = exports.arrayEachTruthyComparedToLast = exports.eachTruthyComparedToLast = exports.arrayTouch = exports.wrapIfNotArray = void 0;
/**

@@ -18,2 +18,4 @@ * If the provided value is not an array,

exports.wrapIfNotArray = wrapIfNotArray;
/** @alias wrapIfNotArray */
exports.arrayTouch = wrapIfNotArray;
/**

@@ -34,2 +36,4 @@ * Return `true` if the `comparison` function returns true

exports.eachTruthyComparedToLast = eachTruthyComparedToLast;
/** @alias eachTruthyComparedToLast */
exports.arrayEachTruthyComparedToLast = eachTruthyComparedToLast;
/**

@@ -42,2 +46,4 @@ * Return true if each value is greater than the last

exports.valuesAreIncreasing = valuesAreIncreasing;
/** @alias valuesAreIncreasing */
exports.arrayValuesAreIncreasing = valuesAreIncreasing;
/**

@@ -50,2 +56,4 @@ * Return true if each value is greater than the last

exports.valuesAreDecreasing = valuesAreDecreasing;
/** @alias valuesAreDecreasing */
exports.arrayValuesAreDecreasing = valuesAreDecreasing;
/**

@@ -64,3 +72,37 @@ * If not an array, return self. Otherwise return 0th item.

exports.selfOrFirstItem = selfOrFirstItem;
/** @alias selfOrFirstItem */
exports.arrayUntouch = selfOrFirstItem;
function sortResult(numbersOrArrayItem1, array2ItemOrDescending, descending = false) {
if (typeof numbersOrArrayItem1 == 'number') {
if (typeof array2ItemOrDescending != 'number') {
throw new Error('Second argument must be a number');
}
const diff = numbersOrArrayItem1 - array2ItemOrDescending;
// @ts-ignore
return descending ? -diff : diff;
}
else if (Array.isArray(numbersOrArrayItem1)) {
// @ts-ignore
return numbersOrArrayItem1.sort((a, b) => sortResult(a, b, descending));
}
throw new Error('Invalid arguments for arraySortNumeric');
}
function arraySortNumeric(numbersOrArrayItem1, array2Item) {
// @ts-ignore
return sortResult(numbersOrArrayItem1, array2Item);
}
exports.arraySortNumeric = arraySortNumeric;
function arraySortNumericDescending(numbersOrArrayItem1, array2Item) {
// @ts-ignore
return sortResult(numbersOrArrayItem1, array2Item, true);
}
exports.arraySortNumericDescending = arraySortNumericDescending;
exports.array = {
arrayTouch: exports.arrayTouch,
arrayEachTruthyComparedToLast: exports.arrayEachTruthyComparedToLast,
arrayUntouch: exports.arrayUntouch,
arrayValuesAreDecreasing: exports.arrayValuesAreDecreasing,
arrayValuesAreIncreasing: exports.arrayValuesAreIncreasing,
arraySortNumeric,
arraySortNumericDescending,
eachTruthyComparedToLast,

@@ -67,0 +109,0 @@ selfOrFirstItem,

export declare function isValidDate(date: Date): boolean;
/** @alias isValidDate */
export declare const dateIsValid: typeof isValidDate;
export declare function assertValidDate(date: Date): void;
/** @alias assertValidDate */
export declare const dateAssertIsValid: typeof assertValidDate;
/** Positive if date2 is in the past. */
export declare function dateDifferenceMillis(date1: Date, date2: Date): number;
/** Positive if date2 is in the past. */
export declare function dateDifferenceSeconds(date1: Date, date2: Date): number;
/** Positive if date2 is in the past. */
export declare function dateDifferenceMinutes(date1: Date, date2: Date): number;
/** Positive if date2 is in the past. */
export declare function dateDifferenceHours(date1: Date, date2: Date): number;
/** Positive if date2 is in the past. */
export declare function dateDifferenceDays(date1: Date, date2: Date): number;
export declare function dateIsOlderThanMillisAgo(date: Date, millisAgo: number): boolean;
export declare function dateIsOlderThanSecondsAgo(date: Date, secondsAgo: number): boolean;

@@ -12,7 +27,15 @@ export declare function dateIsOlderThanMinutesAgo(date: Date, minutes?: number): boolean;

export declare function chronologySort(date1: Date, date2: Date): number;
/** @alias chronologySort */
export declare const dateSort: typeof chronologySort;
export declare function chronologySortReverse(date1: Date, date2: Date): number;
/** @alias chronologySortReverse */
export declare const dateSortDescending: typeof chronologySortReverse;
export declare const dates: {
assertValidDate: typeof assertValidDate;
chronologySort: typeof chronologySort;
chronologySortReverse: typeof chronologySortReverse;
dateSort: typeof chronologySort;
dateSortDescending: typeof chronologySortReverse;
dateDifferenceMillis: typeof dateDifferenceMillis;
dateDifferenceSeconds: typeof dateDifferenceSeconds;
dateDifferenceMinutes: typeof dateDifferenceMinutes;
dateDifferenceHours: typeof dateDifferenceHours;
dateDifferenceDays: typeof dateDifferenceDays;
dateIsGreaterThan: typeof dateIsGreaterThan;

@@ -22,8 +45,14 @@ dateIsInTheFuture: typeof dateIsInTheFuture;

dateIsLessThan: typeof dateIsLessThan;
dateIsOlderThanMillisAgo: typeof dateIsOlderThanMillisAgo;
dateIsOlderThanSecondsAgo: typeof dateIsOlderThanSecondsAgo;
dateIsOlderThanMinutesAgo: typeof dateIsOlderThanMinutesAgo;
dateIsOlderThanHoursAgo: typeof dateIsOlderThanHoursAgo;
dateIsOlderThanDaysAgo: typeof dateIsOlderThanDaysAgo;
dateIsOlderThanHoursAgo: typeof dateIsOlderThanHoursAgo;
dateIsOlderThanMinutesAgo: typeof dateIsOlderThanMinutesAgo;
dateIsOlderThanSecondsAgo: typeof dateIsOlderThanSecondsAgo;
dateIsValid: typeof isValidDate;
dateAssertIsValid: typeof assertValidDate;
isValidDate: typeof isValidDate;
assertValidDate: typeof assertValidDate;
chronologySort: typeof chronologySort;
chronologySortReverse: typeof chronologySortReverse;
};
//# sourceMappingURL=dates.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dates = exports.chronologySortReverse = exports.chronologySort = exports.dateIsLessThan = exports.dateIsGreaterThan = exports.dateIsInThePast = exports.dateIsInTheFuture = exports.dateIsOlderThanDaysAgo = exports.dateIsOlderThanHoursAgo = exports.dateIsOlderThanMinutesAgo = exports.dateIsOlderThanSecondsAgo = exports.assertValidDate = exports.isValidDate = void 0;
exports.dates = exports.dateSortDescending = exports.chronologySortReverse = exports.dateSort = exports.chronologySort = exports.dateIsLessThan = exports.dateIsGreaterThan = exports.dateIsInThePast = exports.dateIsInTheFuture = exports.dateIsOlderThanDaysAgo = exports.dateIsOlderThanHoursAgo = exports.dateIsOlderThanMinutesAgo = exports.dateIsOlderThanSecondsAgo = exports.dateIsOlderThanMillisAgo = exports.dateDifferenceDays = exports.dateDifferenceHours = exports.dateDifferenceMinutes = exports.dateDifferenceSeconds = exports.dateDifferenceMillis = exports.dateAssertIsValid = exports.assertValidDate = exports.dateIsValid = exports.isValidDate = void 0;
const errors_1 = require("./errors");

@@ -10,2 +10,4 @@ function isValidDate(date) {

exports.isValidDate = isValidDate;
/** @alias isValidDate */
exports.dateIsValid = isValidDate;
function assertValidDate(date) {

@@ -15,7 +17,35 @@ errors_1.assert(isValidDate(date), `${date} is not a date`);

exports.assertValidDate = assertValidDate;
/** @alias assertValidDate */
exports.dateAssertIsValid = assertValidDate;
/** Positive if date2 is in the past. */
function dateDifferenceMillis(date1, date2) {
return date1.getTime() - date2.getTime();
}
exports.dateDifferenceMillis = dateDifferenceMillis;
/** Positive if date2 is in the past. */
function dateDifferenceSeconds(date1, date2) {
return dateDifferenceMillis(date1, date2) / 1000;
}
exports.dateDifferenceSeconds = dateDifferenceSeconds;
/** Positive if date2 is in the past. */
function dateDifferenceMinutes(date1, date2) {
return dateDifferenceSeconds(date1, date2) / 60;
}
exports.dateDifferenceMinutes = dateDifferenceMinutes;
/** Positive if date2 is in the past. */
function dateDifferenceHours(date1, date2) {
return dateDifferenceMinutes(date1, date2) / 60;
}
exports.dateDifferenceHours = dateDifferenceHours;
/** Positive if date2 is in the past. */
function dateDifferenceDays(date1, date2) {
return dateDifferenceHours(date1, date2) / 24;
}
exports.dateDifferenceDays = dateDifferenceDays;
function dateIsOlderThanMillisAgo(date, millisAgo) {
return dateDifferenceMillis(new Date(), date) > millisAgo;
}
exports.dateIsOlderThanMillisAgo = dateIsOlderThanMillisAgo;
function dateIsOlderThanSecondsAgo(date, secondsAgo) {
const nowInMilliseconds = Date.now();
const dateInMilliseconds = (date && date.getTime && date.getTime()) || 0;
const millisecondsAgo = secondsAgo * 1000;
return nowInMilliseconds - millisecondsAgo > dateInMilliseconds;
return dateIsOlderThanMillisAgo(date, secondsAgo * 1000);
}

@@ -65,2 +95,4 @@ exports.dateIsOlderThanSecondsAgo = dateIsOlderThanSecondsAgo;

exports.chronologySort = chronologySort;
/** @alias chronologySort */
exports.dateSort = chronologySort;
function chronologySortReverse(date1, date2) {

@@ -70,6 +102,12 @@ return chronologySort(date2, date1);

exports.chronologySortReverse = chronologySortReverse;
/** @alias chronologySortReverse */
exports.dateSortDescending = chronologySortReverse;
exports.dates = {
assertValidDate,
chronologySort,
chronologySortReverse,
dateSort: exports.dateSort,
dateSortDescending: exports.dateSortDescending,
dateDifferenceMillis,
dateDifferenceSeconds,
dateDifferenceMinutes,
dateDifferenceHours,
dateDifferenceDays,
dateIsGreaterThan,

@@ -79,8 +117,14 @@ dateIsInTheFuture,

dateIsLessThan,
dateIsOlderThanMillisAgo,
dateIsOlderThanSecondsAgo,
dateIsOlderThanMinutesAgo,
dateIsOlderThanHoursAgo,
dateIsOlderThanDaysAgo,
dateIsOlderThanHoursAgo,
dateIsOlderThanMinutesAgo,
dateIsOlderThanSecondsAgo,
dateIsValid: exports.dateIsValid,
dateAssertIsValid: exports.dateAssertIsValid,
isValidDate,
assertValidDate,
chronologySort,
chronologySortReverse,
};
//# sourceMappingURL=dates.js.map
/** Get a promise that resolves in some number of milliseconds. */
export declare function resolveInMillis(millis: number): Promise<unknown>;
/** @alias resolveInMillis */
export declare const waitForMillis: typeof resolveInMillis;
/** @alias resolveInMillis */
export declare const wait: typeof resolveInMillis;
/** Get a promise that resolves in some number of seconds. */
export declare function resolveInSeconds(seconds: number): Promise<unknown>;
/** @alias resolveInSeconds */
export declare const waitForSeconds: typeof resolveInSeconds;
export declare function resolveInNextTick(): Promise<unknown>;
export declare function wait(millis: number): Promise<unknown>;
/** @alias resolveInNextTick */
export declare const waitForTick: typeof resolveInNextTick;
export declare const waits: {

@@ -11,3 +18,7 @@ resolveInMillis: typeof resolveInMillis;

resolveInNextTick: typeof resolveInNextTick;
wait: typeof resolveInMillis;
waitForMillis: typeof resolveInMillis;
waitForSeconds: typeof resolveInSeconds;
waitForTick: typeof resolveInNextTick;
};
//# sourceMappingURL=wait.d.ts.map

18

build/lib/wait.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.waits = exports.wait = exports.resolveInNextTick = exports.resolveInSeconds = exports.resolveInMillis = void 0;
exports.waits = exports.waitForTick = exports.resolveInNextTick = exports.waitForSeconds = exports.resolveInSeconds = exports.wait = exports.waitForMillis = exports.resolveInMillis = void 0;
/** Get a promise that resolves in some number of milliseconds. */

@@ -9,2 +9,6 @@ function resolveInMillis(millis) {

exports.resolveInMillis = resolveInMillis;
/** @alias resolveInMillis */
exports.waitForMillis = resolveInMillis;
/** @alias resolveInMillis */
exports.wait = resolveInMillis;
/** Get a promise that resolves in some number of seconds. */

@@ -15,2 +19,4 @@ function resolveInSeconds(seconds) {

exports.resolveInSeconds = resolveInSeconds;
/** @alias resolveInSeconds */
exports.waitForSeconds = resolveInSeconds;
function resolveInNextTick() {

@@ -20,6 +26,4 @@ return new Promise((res) => setImmediate(res));

exports.resolveInNextTick = resolveInNextTick;
function wait(millis) {
return resolveInMillis(millis);
}
exports.wait = wait;
/** @alias resolveInNextTick */
exports.waitForTick = resolveInNextTick;
exports.waits = {

@@ -29,3 +33,7 @@ resolveInMillis,

resolveInNextTick,
wait: exports.wait,
waitForMillis: exports.waitForMillis,
waitForSeconds: exports.waitForSeconds,
waitForTick: exports.waitForTick,
};
//# sourceMappingURL=wait.js.map

@@ -0,1 +1,10 @@

# [0.16.0](https://github.com/bscotch/node-util/compare/v0.15.0...v0.16.0) (2021-04-08)
### Features
* Add more date functions, numeric array sorting functions, and missing test cases. ([2a8f7b0](https://github.com/bscotch/node-util/commit/2a8f7b08886e794a2fa0fb361c277aa096a215e2))
# [0.15.0](https://github.com/bscotch/node-util/compare/v0.14.0...v0.15.0) (2021-03-17)

@@ -2,0 +11,0 @@

{
"name": "@bscotch/utility",
"version": "0.15.0",
"version": "0.16.0",
"description": "Bscotch Utilities: Methods for common Node.js needs.",

@@ -5,0 +5,0 @@ "engines": {

@@ -176,5 +176,5 @@ # Bscotch Utilities

import {
resolveInMillis,
resolveInSeconds,
resolveInNextTick,
waitForMillis,
waitForSeconds,
waitForTick,
} from '@bscotch/utility';

@@ -184,7 +184,7 @@

// Wait for 1 second
await resolveInMillis(1000);
await waitForMillis(1000);
// Wait for 1 second
await resolveInSeconds(1);
await waitForSeconds(1);
// Wait until next tick
await resolveInNextTick();
await waitForTick();
}

@@ -265,3 +265,14 @@ ```

import {
isValidDate,
dateSort,
dateSortDescending,
dateDifferenceMillis,
dateDifferenceSeconds,
dateDifferenceMinutes,
dateDifferenceHours,
dateDifferenceDays,
dateIsGreaterThan,
dateIsInTheFuture,
dateIsInThePast,
dateIsLessThan,
dateIsOlderThanMillisAgo,
dateIsOlderThanSecondsAgo,

@@ -271,8 +282,4 @@ dateIsOlderThanMinutesAgo,

dateIsOlderThanDaysAgo,
dateIsInTheFuture,
dateIsInThePast,
dateIsGreaterThan,
dateIsLessThan,
chronologySortReverse,
chronologySort
dateIsValid,
dateAssertIsValid,
} from '@bscotch/utility';

@@ -285,17 +292,20 @@ ```

import {
wrapIfNotArray,
valuesAreIncreasing,
valuesAreDecreasing,
selfOrFirstItem,
arrayTouch,
arrayUntouch,
arrayEachTruthyComparedToLast,
arrayValuesAreDecreasing,
arrayValuesAreIncreasing,
arraySortNumeric,
arraySortNumericDescending,
} from '@bscotch/utility';
wrapIfNotArray( "hello" ); // => ["hello"]
wrapIfNotArray(["hello"]); // => ["hello"]
wrapIfNotArray( undefined ); // => []
arrayTouch( "hello" ); // => ["hello"]
arrayTouch(["hello"]); // => ["hello"]
arrayTouch( undefined ); // => []
valuesAreIncreasing([-10,99,1111]); // => true
arrayValuesAreIncreasing([-10,99,1111]); // => true
selfOrFirstItem( "hello" ); // => "hello"
selfOrFirstItem(["hello"]); // => "hello"
selfOrFirstItem(["hello","goodbye"]); // => "hello"
```git
arrayUntouch( "hello" ); // => "hello"
arrayUntouch(["hello"]); // => "hello"
arrayUntouch(["hello","goodbye"]); // => "hello"
```

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

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