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

@thi.ng/arrays

Package Overview
Dependencies
Maintainers
1
Versions
190
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@thi.ng/arrays - npm Package Compare versions

Comparing version 0.2.5 to 0.3.0

api.d.ts

9

binary-search.d.ts
import { Fn } from "@thi.ng/api";
/**
* Returns the supposed index of `x` in pre-sorted array-like collection
* `buf`. If `x` can't be found, returns `-index-1`.
* `buf`. If `x` can't be found, returns `-index-1`, representing the
* negative of the index were `x` to be inserted into `buf`. E.g if the
* return value is -3, `x` would appear/insert at index 2.
*
* ```
* binarySearch([2, 4, 6], 5);
* // -3
* ```
*
* The optional `key` function is used to obtain the actual sort value

@@ -7,0 +14,0 @@ * of `x` and each array item (default: identity).

import { compare } from "@thi.ng/compare";
/**
* Returns the supposed index of `x` in pre-sorted array-like collection
* `buf`. If `x` can't be found, returns `-index-1`.
* `buf`. If `x` can't be found, returns `-index-1`, representing the
* negative of the index were `x` to be inserted into `buf`. E.g if the
* return value is -3, `x` would appear/insert at index 2.
*
* ```
* binarySearch([2, 4, 6], 5);
* // -3
* ```
*
* The optional `key` function is used to obtain the actual sort value

@@ -7,0 +14,0 @@ * of `x` and each array item (default: identity).

@@ -6,2 +6,15 @@ # Change Log

# [0.3.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/arrays@0.2.5...@thi.ng/arrays@0.3.0) (2019-11-09)
### Features
* **arrays:** add isSorted() ([65b29f4](https://github.com/thi-ng/umbrella/commit/65b29f487459c535acdbed3890c8a4e27d87ae2c))
* **arrays:** add shuffleRange(), refactor shuffle(), add tests ([1924a05](https://github.com/thi-ng/umbrella/commit/1924a05ea093e3d1d0b3f063cb331b330cee0c0a))
* **arrays:** add types, quickSort(), swap(), multiSwap(), update readme ([b834722](https://github.com/thi-ng/umbrella/commit/b83472237b3ba262dcbb644c8ccc516d0021bc84))
## [0.2.5](https://github.com/thi-ng/umbrella/compare/@thi.ng/arrays@0.2.4...@thi.ng/arrays@0.2.5) (2019-09-21)

@@ -8,0 +21,0 @@

@@ -0,1 +1,14 @@

/**
* Returns true if the last items of `buf` are the same items as in
* `needle`. This means `buf` should have at least the same length as
* `needle` for this to be true.
*
* By default, uses thi.ng/equiv for equality checking.
*
* @see startsWith
*
* @param buf
* @param needle
* @param equiv
*/
export declare const endsWith: <T>(buf: ArrayLike<T>, needle: ArrayLike<T>, equiv?: (a: any, b: any) => boolean) => boolean;
import { equiv as _eq } from "@thi.ng/equiv";
/**
* Returns true if the last items of `buf` are the same items as in
* `needle`. This means `buf` should have at least the same length as
* `needle` for this to be true.
*
* By default, uses thi.ng/equiv for equality checking.
*
* @see startsWith
*
* @param buf
* @param needle
* @param equiv
*/
export const endsWith = (buf, needle, equiv = _eq) => {

@@ -3,0 +16,0 @@ let i = buf.length;

@@ -10,2 +10,9 @@ /**

export declare const ensureArray: (x: any) => any[];
/**
* Similar to `ensureArray()`, but for `ArrayLike` types.
*
* @see ensureArray
*
* @param x
*/
export declare const ensureArrayLike: (x: any) => ArrayLike<any>;

@@ -12,2 +12,9 @@ import { isArray, isArrayLike } from "@thi.ng/checks";

export const ensureArray = (x) => isArray(x) ? x : [...ensureIterable(x)];
/**
* Similar to `ensureArray()`, but for `ArrayLike` types.
*
* @see ensureArray
*
* @param x
*/
export const ensureArrayLike = (x) => isArrayLike(x) ? x : [...ensureIterable(x)];

@@ -1,1 +0,7 @@

export declare const ensureIterable: (x: any) => IterableIterator<any>;
/**
* Attempts to obtain an iterator from `x` and throws error if `x` is
* not iterable.
*
* @param x
*/
export declare const ensureIterable: (x: any) => Iterable<any>;

9

ensure-iterable.js
import { illegalArgs } from "@thi.ng/errors";
/**
* Attempts to obtain an iterator from `x` and throws error if `x` is
* not iterable.
*
* @param x
*/
export const ensureIterable = (x) => {
if (!(x != null && x[Symbol.iterator])) {
(x == null || !x[Symbol.iterator]) &&
illegalArgs(`value is not iterable: ${x}`);
}
return x;
};

@@ -7,5 +7,8 @@ export * from "./binary-search";

export * from "./fuzzy-match";
export * from "./is-sorted";
export * from "./peek";
export * from "./quicksort";
export * from "./shuffle";
export * from "./starts-with";
export * from "./swap";
export * from "./swizzle";

@@ -7,5 +7,8 @@ export * from "./binary-search";

export * from "./fuzzy-match";
export * from "./is-sorted";
export * from "./peek";
export * from "./quicksort";
export * from "./shuffle";
export * from "./starts-with";
export * from "./swap";
export * from "./swizzle";

@@ -9,2 +9,3 @@ 'use strict';

var errors = require('@thi.ng/errors');
var api = require('@thi.ng/api');
var random = require('@thi.ng/random');

@@ -42,5 +43,4 @@

const ensureIterable = (x) => {
if (!(x != null && x[Symbol.iterator])) {
(x == null || !x[Symbol.iterator]) &&
errors.illegalArgs(`value is not iterable: ${x}`);
}
return x;

@@ -85,12 +85,83 @@ };

const isSorted = (arr, cmp = compare.compare, start = 0, end = arr.length) => {
let prev = arr[start];
while (++start < end) {
const curr = arr[start];
if (cmp(prev, curr) > 0)
return false;
prev = curr;
}
return true;
};
const peek = (x) => x[x.length - 1];
const shuffle = (buf, n = buf.length, rnd = random.SYSTEM) => {
n = Math.min(n, buf.length);
const swap = (arr, x, y) => {
const t = arr[x];
arr[x] = arr[y];
arr[y] = t;
};
const multiSwap = (...xs) => {
const [b, c, d] = xs;
const n = xs.length;
switch (n) {
case 0:
return swap;
case 1:
return (a, x, y) => {
swap(a, x, y);
swap(b, x, y);
};
case 2:
return (a, x, y) => {
swap(a, x, y);
swap(b, x, y);
swap(c, x, y);
};
case 3:
return (a, x, y) => {
swap(a, x, y);
swap(b, x, y);
swap(c, x, y);
swap(d, x, y);
};
default:
return (a, x, y) => {
swap(a, x, y);
for (let i = n; --i >= 0;)
swap(xs[i], x, y);
};
}
};
function quickSort(arr, _cmp = compare.compare, _swap = swap, start = 0, end = arr.length - 1) {
if (start < end) {
const pivot = arr[start + ((end - start) >> 1)];
let s = start - 1;
let e = end + 1;
while (true) {
do {
s++;
} while (_cmp(arr[s], pivot) < 0);
do {
e--;
} while (_cmp(arr[e], pivot) > 0);
if (s >= e)
break;
_swap(arr, s, e);
}
quickSort(arr, _cmp, _swap, start, e);
quickSort(arr, _cmp, _swap, e + 1, end);
}
return arr;
}
const shuffleRange = (buf, start = 0, end = buf.length, rnd = random.SYSTEM) => {
api.assert(start >= 0 && end >= start && end <= buf.length, `illegal range ${start}..${end}`);
let n = end - start;
const l = n;
if (l > 1) {
n = Math.min(n, l);
while (--n >= 0) {
const a = rnd.float(l) | 0;
const b = rnd.float(l) | 0;
const a = (start + rnd.float(l)) | 0;
const b = (start + rnd.float(l)) | 0;
const t = buf[a];

@@ -103,2 +174,3 @@ buf[a] = buf[b];

};
const shuffle = (buf, n = buf.length, rnd = random.SYSTEM) => shuffleRange(buf, 0, n, rnd);

@@ -154,5 +226,10 @@ const startsWith = (buf, needle, equiv$1 = equiv.equiv) => {

exports.fuzzyMatch = fuzzyMatch;
exports.isSorted = isSorted;
exports.multiSwap = multiSwap;
exports.peek = peek;
exports.quickSort = quickSort;
exports.shuffle = shuffle;
exports.shuffleRange = shuffleRange;
exports.startsWith = startsWith;
exports.swap = swap;
exports.swizzle = swizzle;

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

!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@thi.ng/compare"),require("@thi.ng/equiv"),require("@thi.ng/checks"),require("@thi.ng/errors"),require("@thi.ng/random")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/compare","@thi.ng/equiv","@thi.ng/checks","@thi.ng/errors","@thi.ng/random"],r):r(((e=e||self).thi=e.thi||{},e.thi.ng=e.thi.ng||{},e.thi.ng.arrays={}),e.thi.ng.compare,e.thi.ng.equiv,e.thi.ng.checks,e.thi.ng.errors,e.thi.ng.random)}(this,function(e,r,t,n,i,u){"use strict";const o=e=>(null!=e&&e[Symbol.iterator]||i.illegalArgs(`value is not iterable: ${e}`),e),s=(e,r,n=t.equiv)=>{for(let t=e.length;--t>=0;)if(n(r,e[t]))return t;return-1};e.binarySearch=(e,t,n=(e=>e),i=r.compare)=>{const u=n(t);let o=0,s=e.length-1;for(;o<=s;){const r=o+s>>>1,t=i(n(e[r]),u);if(t<0)o=r+1;else{if(!(t>0))return r;s=r-1}}return-o-1},e.endsWith=(e,r,n=t.equiv)=>{let i=e.length,u=r.length;if(i<u)return!1;for(;--i,--u>=0&&n(e[i],r[u]););return u<0},e.ensureArray=e=>n.isArray(e)?e:[...o(e)],e.ensureArrayLike=e=>n.isArrayLike(e)?e:[...o(e)],e.ensureIterable=o,e.find=(e,r,n=t.equiv)=>{const i=s(e,r,n);return-1!==i?e[i]:void 0},e.findIndex=s,e.fuzzyMatch=(e,r,n=t.equiv)=>{const i=e.length,u=r.length;if(u>i)return!1;if(u===i)return n(r,e);e:for(let t=0,o=0;t<u;t++){const u=r[t];for(;o<i;)if(n(e[o++],u))continue e;return!1}return!0},e.peek=e=>e[e.length-1],e.shuffle=(e,r=e.length,t=u.SYSTEM)=>{const n=r=Math.min(r,e.length);if(n>1)for(r=Math.min(r,n);--r>=0;){const r=0|t.float(n),i=0|t.float(n),u=e[r];e[r]=e[i],e[i]=u}return e},e.startsWith=(e,r,n=t.equiv)=>{let i=e.length,u=r.length;if(i<u)return!1;for(;-u>=0&&n(e[u],r[u]););return u<0},e.swizzle=e=>{const[r,t,n,i,u,o,s,h]=e;switch(e.length){case 0:return()=>[];case 1:return e=>[e[r]];case 2:return e=>[e[r],e[t]];case 3:return e=>[e[r],e[t],e[n]];case 4:return e=>[e[r],e[t],e[n],e[i]];case 5:return e=>[e[r],e[t],e[n],e[i],e[u]];case 6:return e=>[e[r],e[t],e[n],e[i],e[u],e[o]];case 7:return e=>[e[r],e[t],e[n],e[i],e[u],e[o],e[s]];case 8:return e=>[e[r],e[t],e[n],e[i],e[u],e[o],e[s],e[h]];default:return r=>{const t=[];for(let n=e.length;--n>=0;)t[n]=r[e[n]];return t}}},Object.defineProperty(e,"__esModule",{value:!0})});
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@thi.ng/compare"),require("@thi.ng/equiv"),require("@thi.ng/checks"),require("@thi.ng/errors"),require("@thi.ng/api"),require("@thi.ng/random")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/compare","@thi.ng/equiv","@thi.ng/checks","@thi.ng/errors","@thi.ng/api","@thi.ng/random"],r):r(((e=e||self).thi=e.thi||{},e.thi.ng=e.thi.ng||{},e.thi.ng.arrays={}),e.thi.ng.compare,e.thi.ng.equiv,e.thi.ng.checks,e.thi.ng.errors,e.thi.ng.api,e.thi.ng.random)}(this,(function(e,r,t,n,i,u,o){"use strict";const s=e=>((null==e||!e[Symbol.iterator])&&i.illegalArgs(`value is not iterable: ${e}`),e),h=(e,r,n=t.equiv)=>{for(let t=e.length;--t>=0;)if(n(r,e[t]))return t;return-1},l=(e,r,t)=>{const n=e[r];e[r]=e[t],e[t]=n};const a=(e,r=0,t=e.length,n=o.SYSTEM)=>{u.assert(r>=0&&t>=r&&t<=e.length,`illegal range ${r}..${t}`);let i=t-r;const s=i;if(s>1)for(;--i>=0;){const t=r+n.float(s)|0,i=r+n.float(s)|0,u=e[t];e[t]=e[i],e[i]=u}return e};e.binarySearch=(e,t,n=(e=>e),i=r.compare)=>{const u=n(t);let o=0,s=e.length-1;for(;o<=s;){const r=o+s>>>1,t=i(n(e[r]),u);if(t<0)o=r+1;else{if(!(t>0))return r;s=r-1}}return-o-1},e.endsWith=(e,r,n=t.equiv)=>{let i=e.length,u=r.length;if(i<u)return!1;for(;--i,--u>=0&&n(e[i],r[u]););return u<0},e.ensureArray=e=>n.isArray(e)?e:[...s(e)],e.ensureArrayLike=e=>n.isArrayLike(e)?e:[...s(e)],e.ensureIterable=s,e.find=(e,r,n=t.equiv)=>{const i=h(e,r,n);return-1!==i?e[i]:void 0},e.findIndex=h,e.fuzzyMatch=(e,r,n=t.equiv)=>{const i=e.length,u=r.length;if(u>i)return!1;if(u===i)return n(r,e);e:for(let t=0,o=0;t<u;t++){const u=r[t];for(;o<i;)if(n(e[o++],u))continue e;return!1}return!0},e.isSorted=(e,t=r.compare,n=0,i=e.length)=>{let u=e[n];for(;++n<i;){const r=e[n];if(t(u,r)>0)return!1;u=r}return!0},e.multiSwap=(...e)=>{const[r,t,n]=e,i=e.length;switch(i){case 0:return l;case 1:return(e,t,n)=>{l(e,t,n),l(r,t,n)};case 2:return(e,n,i)=>{l(e,n,i),l(r,n,i),l(t,n,i)};case 3:return(e,i,u)=>{l(e,i,u),l(r,i,u),l(t,i,u),l(n,i,u)};default:return(r,t,n)=>{l(r,t,n);for(let r=i;--r>=0;)l(e[r],t,n)}}},e.peek=e=>e[e.length-1],e.quickSort=function e(t,n=r.compare,i=l,u=0,o=t.length-1){if(u<o){const r=t[u+(o-u>>1)];let s=u-1,h=o+1;for(;;){do{s++}while(n(t[s],r)<0);do{h--}while(n(t[h],r)>0);if(s>=h)break;i(t,s,h)}e(t,n,i,u,h),e(t,n,i,h+1,o)}return t},e.shuffle=(e,r=e.length,t=o.SYSTEM)=>a(e,0,r,t),e.shuffleRange=a,e.startsWith=(e,r,n=t.equiv)=>{let i=e.length,u=r.length;if(i<u)return!1;for(;-u>=0&&n(e[u],r[u]););return u<0},e.swap=l,e.swizzle=e=>{const[r,t,n,i,u,o,s,h]=e;switch(e.length){case 0:return()=>[];case 1:return e=>[e[r]];case 2:return e=>[e[r],e[t]];case 3:return e=>[e[r],e[t],e[n]];case 4:return e=>[e[r],e[t],e[n],e[i]];case 5:return e=>[e[r],e[t],e[n],e[i],e[u]];case 6:return e=>[e[r],e[t],e[n],e[i],e[u],e[o]];case 7:return e=>[e[r],e[t],e[n],e[i],e[u],e[o],e[s]];case 8:return e=>[e[r],e[t],e[n],e[i],e[u],e[o],e[s],e[h]];default:return r=>{const t=[];for(let n=e.length;--n>=0;)t[n]=r[e[n]];return t}}},Object.defineProperty(e,"__esModule",{value:!0})}));
{
"name": "@thi.ng/arrays",
"version": "0.2.5",
"version": "0.3.0",
"description": "Array / Arraylike utilities",

@@ -32,12 +32,12 @@ "module": "./index.js",

"nyc": "^14.0.0",
"typedoc": "^0.14.2",
"typescript": "^3.5.3"
"typedoc": "^0.15.0",
"typescript": "^3.6.4"
},
"dependencies": {
"@thi.ng/api": "^6.4.0",
"@thi.ng/checks": "^2.4.0",
"@thi.ng/compare": "^1.0.9",
"@thi.ng/equiv": "^1.0.9",
"@thi.ng/errors": "^1.2.0",
"@thi.ng/random": "^1.1.12"
"@thi.ng/api": "^6.5.0",
"@thi.ng/checks": "^2.4.1",
"@thi.ng/compare": "^1.0.10",
"@thi.ng/equiv": "^1.0.10",
"@thi.ng/errors": "^1.2.1",
"@thi.ng/random": "^1.1.13"
},

@@ -57,3 +57,3 @@ "keywords": [

"sideEffects": false,
"gitHead": "5f865588e37a27ceb46484c24fce4d59a0637f90"
"gitHead": "97add769f24aa32a1a5e13c5c941605e1b9eb569"
}

@@ -54,5 +54,10 @@ # @thi.ng/arrays

- [fuzzyMatch()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/fuzzy-match.ts)
- [isSorted()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/is-sorted.ts)
- [multiSwap()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/swap.ts)
- [peek()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/peek.ts)
- [quickSort()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/quicksort.ts)
- [shuffle()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/shuffle.ts) (w/ custom PRNG support)
- [shuffleRange()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/shuffle.ts) (w/ custom PRNG support)
- [startsWith()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/starts-with.ts)
- [swap()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/swap.ts)
- [swizzle()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/swizzle.ts)

@@ -59,0 +64,0 @@

import { IRandom } from "@thi.ng/random";
import { AnyArray } from "./api";
/**
* Shuffles the first `n` items of given array, using Fisher-yates and
* optional `rnd` PRNG. If `n` is `undefined`, the entire array will be
* shuffled.
* Shuffles the items in the given index range of array `buf` using
* Fisher-yates and optional `rnd` PRNG. If neither `start` / `end` are
* given, the entire array will be shuffled. Mutates original array.
*
* @param buf
* @param n
* @param rnd
*/
export declare const shuffleRange: <T extends AnyArray>(buf: T, start?: number, end?: number, rnd?: IRandom) => T;
/**
* Applies `shuffleRange()` to the given array. If `n` is given, only
* the first `n` items are shuffled. Mutates original array.
*
* @see shuffleRange
*
* @param buf

@@ -12,2 +23,2 @@ * @param n

*/
export declare const shuffle: (buf: any[], n?: number, rnd?: IRandom) => any[];
export declare const shuffle: <T extends AnyArray>(buf: T, n?: number, rnd?: IRandom) => T;

@@ -0,8 +1,8 @@

import { assert } from "@thi.ng/api";
import { SYSTEM } from "@thi.ng/random";
/**
* Shuffles the first `n` items of given array, using Fisher-yates and
* optional `rnd` PRNG. If `n` is `undefined`, the entire array will be
* shuffled.
* Shuffles the items in the given index range of array `buf` using
* Fisher-yates and optional `rnd` PRNG. If neither `start` / `end` are
* given, the entire array will be shuffled. Mutates original array.
*
*
* @param buf

@@ -12,10 +12,10 @@ * @param n

*/
export const shuffle = (buf, n = buf.length, rnd = SYSTEM) => {
n = Math.min(n, buf.length);
export const shuffleRange = (buf, start = 0, end = buf.length, rnd = SYSTEM) => {
assert(start >= 0 && end >= start && end <= buf.length, `illegal range ${start}..${end}`);
let n = end - start;
const l = n;
if (l > 1) {
n = Math.min(n, l);
while (--n >= 0) {
const a = rnd.float(l) | 0;
const b = rnd.float(l) | 0;
const a = (start + rnd.float(l)) | 0;
const b = (start + rnd.float(l)) | 0;
const t = buf[a];

@@ -28,1 +28,12 @@ buf[a] = buf[b];

};
/**
* Applies `shuffleRange()` to the given array. If `n` is given, only
* the first `n` items are shuffled. Mutates original array.
*
* @see shuffleRange
*
* @param buf
* @param n
* @param rnd
*/
export const shuffle = (buf, n = buf.length, rnd = SYSTEM) => shuffleRange(buf, 0, n, rnd);

@@ -0,1 +1,14 @@

/**
* Returns true if the first items of `buf` are the same items as in
* `needle`. This means `buf` should have at least the same length as
* `needle` for this to be true.
*
* By default, uses thi.ng/equiv for equality checking.
*
* @see endsWith
*
* @param buf
* @param needle
* @param equiv
*/
export declare const startsWith: (buf: ArrayLike<any>, needle: ArrayLike<any>, equiv?: (a: any, b: any) => boolean) => boolean;
import { equiv as _eq } from "@thi.ng/equiv";
/**
* Returns true if the first items of `buf` are the same items as in
* `needle`. This means `buf` should have at least the same length as
* `needle` for this to be true.
*
* By default, uses thi.ng/equiv for equality checking.
*
* @see endsWith
*
* @param buf
* @param needle
* @param equiv
*/
export const startsWith = (buf, needle, equiv = _eq) => {

@@ -3,0 +16,0 @@ let i = buf.length;

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