New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

@locustjs/extensions-array

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@locustjs/extensions-array - npm Package Compare versions

Comparing version
2.4.0
to
2.4.2
+41
tests/Array.find.js
import { configureArrayExtensions } from "../src";
import { equals } from "@locustjs/base";
configureArrayExtensions({ include: "find", force: true });
const tests = [
[
"Array.find: 1",
function (expect) {
const arr = ["Red", "Green", "Blue", "Black", "White"];
const ec = {
equals: (x, y) => x.toLowerCase() == y.toLowerCase(),
};
const x = arr.find("red", ec);
expect(x).toBeDefined();
expect(x).toBe("Red");
},
],
[
"Array.find: 2",
function (expect) {
const arr = [
{ id: 1, name: "red" },
{ id: 2, name: "blue" },
{ id: 3, name: "green" },
];
const value = { id: "2", name: "blue" };
const ec = {
equals,
};
const x = arr.find(value, ec);
expect(x).toBeDefined();
expect(x.id).toBe(2);
expect(x.name).toBe("blue");
},
],
];
export default tests;
import { configureArrayExtensions } from "../src";
import { equals } from "@locustjs/base";
configureArrayExtensions({ include: "findIndex", force: true });
const tests = [
[
"Array.findIndex: 1",
function (expect) {
const arr = ["Red", "Green", "Blue", "Black", "White"];
const ec = {
equals: (x, y) => x.toLowerCase() == y.toLowerCase(),
};
const x = arr.findIndex("red", ec);
const y = arr.findIndex("GREEN", ec);
const z = arr.findIndex("Orange", ec);
expect(x).toBe(0);
expect(y).toBe(1);
expect(z).toBe(-1);
},
],
[
"Array.findIndex: 2",
function (expect) {
const arr = [
{ id: 1, name: "red" },
{ id: 2, name: "blue" },
{ id: 3, name: "green" },
];
const value = { id: "2", name: "blue" };
const ec = {
equals,
};
const x = arr.findIndex(value, ec);
expect(x).toBe(1);
},
],
];
export default tests;
+17
-11

@@ -368,2 +368,4 @@ 'use strict';

var containsAll = contains;
var _array_find = Array.prototype.find;
var _array_findIndex = Array.prototype.findIndex;
function configureArrayExtensions(options, logger) {

@@ -464,22 +466,27 @@ var eh = new ExtensionHelper(options, logger);

*/
var _array_find = Array.prototype.find;
eh.extend(Array, "find", function (arg, thisArg) {
if (isFunction(arg)) {
var _isEqualityComparer = base.isEqualityComparer(thisArg);
var _this = _isEqualityComparer ? this : thisArg;
if (base.isFunction(arg)) {
return _array_find.call(this, arg, thisArg);
}
return _array_find(function (x) {
return base.isEqualityComparer(thisArg) ? thisArg.equals(x, arg) : x == arg;
}, thisArg);
return _array_find.call(_this, function (x) {
return _isEqualityComparer ? thisArg.equals(x, arg) : x == arg;
});
});
var _array_findIndex = Array.prototype.findIndex;
eh.extend(Array, "findIndex", function (arg, thisArg) {
if (isFunction(arg)) {
var _isEqualityComparer = base.isEqualityComparer(thisArg);
var _this = _isEqualityComparer ? this : thisArg;
if (base.isFunction(arg)) {
return _array_findIndex.call(this, arg, thisArg);
}
return _array_findIndex(function (x) {
return base.isEqualityComparer(thisArg) ? thisArg.equals(x, arg) : x == arg;
}, thisArg);
return _array_findIndex.call(_this, function (x) {
return _isEqualityComparer ? thisArg.equals(x, arg) : x == arg;
});
});
}
exports._array_find = _array_find;
exports._array_findIndex = _array_findIndex;
exports.all = all;

@@ -492,3 +499,2 @@ exports.any = any;

exports.insertAt = insertAt;
exports.joins = joins;
exports.max = max;

@@ -495,0 +501,0 @@ exports.min = min;

{
"name": "@locustjs/extensions-array",
"version": "2.4.0",
"version": "2.4.2",
"description": "This library contains extensions for Array",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

+347
-39
# About
This library contains extensions for Array.
This library contains helpful extensions for arrays. They can be configured as extensions as well to work without being imported or invoked directly on arrays.
## Current Version
```
2.2.0
```
# Install

@@ -26,3 +21,30 @@ ```

## Current Version
```
2.4.2
```
# Configuring as extension methods
Using `configureArrayExtensions()` function, we can configure any of the above functions as an extension method on any array instance.
```javascript
import { configureArrayExtensions } from "@locustjs/extensions-array";
configureArrayExtensions("sortBy,min,max"); // only configure 'sortBy', 'min' and 'max'
```
Configuring array extensions should normally happen at the start of an application.
We can then use the extensions anywhere in our application.
For extending native functions such as `find` and `findIndex`, we need to use the following code:
```javascript
import { configureArrayExtensions } from "@locustjs/extensions-array";
configureArrayExtensions({ include: "find,findIndex", force: true });
```
# Functions
- `clone`
- `shuffle`

@@ -34,9 +56,9 @@ - `range`

- `any`
- `objectify`
- `joins`
- `sortBy`
- `contains`
- `containsAny`
- `min`
- `max`
- `toObject`
- `objectify`

@@ -55,2 +77,11 @@ ## shuffle(array)

As an extension ...
```javascript
const source = [10, 23, 14, 9, 31];
const shuffled = source.shuffle();
console.log(shuffled); // e.g.: 14, 10, 31, 23, 9
```
## range(from, to)

@@ -67,2 +98,10 @@ Generates an array of integer numbers starting from 'from' and ending at 'to - 1'.

As an extension ...
```javascript
const arr = Array.range(5, 10);
console.log(arr); // 5, 6, 7, 8, 9
```
## insertAt(array, index, item)

@@ -81,2 +120,12 @@ Inserts given item at the specified index into an array.

As an extension ...
```javascript
const arr = ['red', 'green'];
arr.insertAt(1, 'blue');
console.log(arr); // 'red', 'blue', 'green'
```
## removeAt(array, index)

@@ -95,2 +144,12 @@ Removes item of the specified index from an array.

As an extension ...
```javascript
const arr = ['red', 'green', 'blue'];
arr.removeAt(1);
console.log(arr); // 'red', 'blue'
```
## all(array, fn)

@@ -114,2 +173,11 @@ Iterates over an array and checks whether all items conform to a condition by calling a given function on each item.

As an extension ...
```javascript
const arr = [10, 20, 30, 40];
console.log(arr.all(x => x % 2 == 0)); // true
console.log(arr.all(x => x < 40)); // false
```
## any(array, fn)

@@ -124,3 +192,3 @@ Iterates over an array and checks if at least one item conforms to a condition by calling given function on each item.

console.log(any(arr, x => x % 2 == 0)); // true
console.log(all(arr, x => x > 40)); // false
console.log(any(arr, x => x > 40)); // false
```

@@ -134,27 +202,9 @@

## objectify(array)
Converts an array into an object.
As an extension ...
```
input:
[
["a", 1],
["b", "ali"]
]
output: { "a": 1, "b": "ali" }
```javascript
const arr = [10, 20, 30, 40];
input:
[
[ ["a",1],["b", "ali"] ],
[ ["a",2],["b", "reza"],["c", true] ],
[ ["a",3],["b"],["c", false] ],
[ ["b", "saeed"],["c", true] ]
]
output:
[
{ "a": 1, "b": "ali" },
{ "a": 2, "b": "reza" , "c": true },
{ "a": 3, "b": null, "c": false },
{ "b": "saeed", "c": true}
]
console.log(arr.any(x => x % 2 == 0)); // true
console.log(arr.any(x => x > 40)); // false
```

@@ -190,5 +240,14 @@

As an extension ...
```javascript
...
const arr2 = arr.sortBy(x => x.parent, x => x.code);
...
```
## contains(array, ...values)
Checks whether given array contains given elements.
Checks whether given array contains given elements. It performs case-insensitive string comparison.
Example 1:
```javascript

@@ -204,5 +263,138 @@ import { contains } from '@locustjs/extensions-array'

As an extension ...
```javascript
...
console.log(arr.contains(23)); // true
console.log(arr.contains(23, 30)); // true
console.log(arr.contains(23, 30, 400)); // false
```
Example 2:
```javascript
import { contains } from '@locustjs/extensions-array'
var arr = [ "Red", "GREEN", "blue" ];
console.log(contains(arr, "red")); // true
console.log(contains(arr, "red", "green")); // true
console.log(contains(arr, "red", "green", "Blue")); // true
console.log(contains(arr, "red", "green", "black")); // false
```
As an extension ...
```javascript
...
console.log(arr.contains("red")); // true
console.log(arr.contains("red", "green")); // true
console.log(arr.contains("red", "green", "Blue")); // true
console.log(arr.contains("red", "green", "black")); // false
```
## containsAny(array, ...values)
Checks whether given array contains at least one of the given elements. It performs case-insensitive string comparison.
Example 1:
```javascript
import { containsAny } from '@locustjs/extensions-array'
var arr = [ 10, 14, 23, 9, 5, 34, 30, 18 ];
console.log(containsAny(arr, 23)); // true
console.log(containsAny(arr, 23, 65)); // true
console.log(containsAny(arr, 28, 44, 100)); // false
```
As an extension ...
```javascript
...
console.log(arr.containsAny(23)); // true
console.log(arr.containsAny(23, 65)); // true
console.log(arr.containsAny(28, 44, 100)); // false
```
Example 2:
```javascript
import { containsAny } from '@locustjs/extensions-array'
var arr = [ "Red", "GREEN", "blue" ];
console.log(containsAny(arr, "red")); // true
console.log(containsAny(arr, "red", "green")); // true
console.log(containsAny(arr, "black", "white", "Blue")); // true
console.log(containsAny(arr, "black", "white", "orange")); // false
```
As an extension ...
```javascript
...
console.log(arr.containsAny("red")); // true
console.log(arr.containsAny("red", "green")); // true
console.log(arr.containsAny("black", "white", "Blue")); // true
console.log(arr.containsAny("black", "white", "orange")); // false
```
## `min(array, fn)`, `max(array, fn)`
Returns minimum or maximum of an array.
Example 1:
```javascript
import { min, max } from '@locustjs/extensions-array'
var arr = [ 10, 8, 19, 3, 5, 11, 21, 4, 17, 9, 15, 16 ];
console.log(min(arr)); // 3
console.log(max(arr)); // 21
```
As an extension ...
```javascript
...
console.log(arr.min()); // 3
console.log(arr.max()); // 21
...
```
Example 2:
```javascript
import { min, max } from '@locustjs/extensions-array'
var arr = [
{ code: 10, name: 'item 1' },
{ code: 8 , name: 'item 2' },
{ code: 19, name: 'item 3' },
{ code: 3 , name: 'item 4' },
{ code: 5 , name: 'item 5' },
{ code: 11, name: 'item 6' },
{ code: 21, name: 'item 7' },
{ code: 4 , name: 'item 8' },
{ code: 17, name: 'item 9' },
{ code: 9 , name: 'item 10'},
{ code: 15, name: 'item 11'},
{ code: 16, name: 'item 12'}
];
const arr2Min = min(arr, x => x.code);
const arr2Max = max(arr, x => x.code);
console.log(`min: ${arr2Min}`);
console.log(`max: ${arr2Max}`);
```
As an extension ...
```javascript
...
const arr2Min = arr.min(x => x.code);
const arr2Max = arr.max(x => x.code);
...
```
## toObject(arr, type, schema?)
This method carries out reverse of `toArray()` method in `@locustjs/extensions-object` library. It converts an array to an object. The result depends on `type` which specifies what type of data the array contains. Possible values are:
This method converts an array of values to an object. The result depends on `type` which specifies what type of data the array contains. Possible values are:

@@ -233,3 +425,6 @@ - `key-value`: it means that `arr` contains both `keys` and `values` (object's prop names and prop values). Each entry in the array is in turn two-elemented array (key and value), the first is the key (prop name) and the second is the value.

const x = toObject(arr, `key-value`); // or arr.toObject(`key-value`) as an extension method
const x = toObject(arr, "key-value");
// as an extension method ...
// const x = arr.toObject("key-value");
console.log(x);

@@ -258,6 +453,10 @@ /*

];
const schema = ["name",["address",["id", "name"]],"age"]; // or x.toArray('schema')
const schema = ["name",["address",["id", "name"]],"age"];
const x = toObject(values, 'values', schema); // or values.toObject('values', schema) as an extension method
const x = toObject(values, 'values', schema);
// as an extension method ...
// const x = values.toObject('values', schema);
console.log(x);
/*

@@ -277,3 +476,7 @@ {

```javascript
const x = toObject(schema, 'schema'); // or schema.toObject('schema') as an extension method
const schema = ["name",["address",["id", "name"]],"age"];
const x = toObject(schema, 'schema');
// as an extension method ...
// const x = schema.toObject('schema');
console.log(x);

@@ -324,2 +527,107 @@ /*

**Note:** `toArray() / toObject()` are similar to `Object.entries() / Object.fromEntries()`. The difference and the benefit is that they perform recursively and produce a more condensced data, whereas `Object.entres()/fromEntries()` do not act recursively (they operate only on the first level).
**Note:** `toArray() / toObject()` are similar to `Object.entries() / Object.fromEntries()`. The difference and the benefit is that they perform recursively and produce a more condensced data, whereas `Object.entres()/fromEntries()` do not act recursively (they operate only on the first level).
## objectify(array)
This function is a shorthand for `toObject(arr, "key-value")` invocation.
```javascript
const objectify = (arr) => toObject(arr, "key-value");
```
It converts an array of key/value items into an object.
```javascript
const arr1 = [
["a", 1],
["b", "ali"]
];
console.log(objectify(arr1));
/*
{ "a": 1, "b": "ali" }
*/
```
As an extension ...
```javascript
...
console.log(arr1.objectify());
```
# Other direct extensions on `Array`
## clone(array)
Clones an array.
```javascript
const arr1 = [10, 23, 14, 9, 31];
const arr2 = arr1.clone();
console.log(arr2);
```
## array.find(value, equalityComparer)
Extends functionality of native `arr.find()` based on the signature mentioned above. i.e. it receives the searched value in the first argument and an `equalityComparer` as the second argument that is used to compare array elements.
An `equalityComparer` is any object that has an `equals(x, y)` method that compares equality of its two arguments (`x` and `y`).
Example 1:
```javascript
const MyStringComparer = {
equals: (x, y) => x.toLowerCase() == y.toLowerCase()
}
const arr = ["Red", "Green", "Blue", "Black", "White"];
const x = arr.find("red", MyStringComparer);
console.log(x); // Red
```
Example 2:
```javascript
import { equals } from "@locustjs/base";
const arr = [
{ id: 1, name: "red" },
{ id: 2, name: "blue" },
{ id: 3, name: "green" },
];
const MyObjectEqualityCompare = { equals };
const value = { id: "2", name: "blue" };
const x = arr.find(value, MyObjectEqualityCompare);
console.log(x); // { id: 2, name: "blue" }
```
For more equalityComparers we can use [@locustjs/compare](https://github.com/ironcodev/locustjs-compare.git) library.
## array.findIndex(value, equalityComparer)
Extends functionality of native `arr.findIndex()` based on the signature mentioned above. i.e. it receives the searched value in the first argument and an `equalityComparer` as the second argument that is used to compare array elements.
Example 1:
```javascript
const MyStringComparer = {
equals: (x, y) => x.toLowerCase() == y.toLowerCase()
}
const arr = ["Red", "Green", "Blue", "Black", "White"];
const x = arr.findIndex("green", MyStringComparer);
console.log(x); // 1
```
Example 2:
```javascript
import { equals } from "@locustjs/base";
const arr = [
{ id: 1, name: "red" },
{ id: 2, name: "blue" },
{ id: 3, name: "green" },
];
const MyObjectEqualityCompare = { equals };
const value = { id: "2", name: "blue" };
const x = arr.findIndex(value, MyObjectEqualityCompare);
console.log(x); // 1
```
Again, for more equalityComparers we can use [@locustjs/compare](https://github.com/ironcodev/locustjs-compare.git) library.

@@ -15,6 +15,8 @@ import ExtensionHelper from "@locustjs/extensions-options";

import toObject from "./toObject";
import { isEqualityComparer } from "@locustjs/base";
import { isEqualityComparer, isFunction } from "@locustjs/base";
const objectify = (arr) => toObject(arr, "key-value");
const containsAll = contains;
const _array_find = Array.prototype.find;
const _array_findIndex = Array.prototype.findIndex;

@@ -115,5 +117,7 @@ function configureArrayExtensions(options, logger) {

*/
const _array_find = Array.prototype.find;
eh.extend(Array, "find", function (arg, thisArg) {
const _isEqualityComparer = isEqualityComparer(thisArg);
const _this = _isEqualityComparer ? this : thisArg;
if (isFunction(arg)) {

@@ -123,11 +127,11 @@ return _array_find.call(this, arg, thisArg);

return _array_find(
(x) => (isEqualityComparer(thisArg) ? thisArg.equals(x, arg) : x == arg),
thisArg
return _array_find.call(_this, (x) =>
_isEqualityComparer ? thisArg.equals(x, arg) : x == arg
);
});
const _array_findIndex = Array.prototype.findIndex;
eh.extend(Array, "findIndex", function (arg, thisArg) {
const _isEqualityComparer = isEqualityComparer(thisArg);
const _this = _isEqualityComparer ? this : thisArg;
eh.extend(Array, "findIndex", function (arg, thisArg) {
if (isFunction(arg)) {

@@ -137,5 +141,4 @@ return _array_findIndex.call(this, arg, thisArg);

return _array_findIndex(
(x) => (isEqualityComparer(thisArg) ? thisArg.equals(x, arg) : x == arg),
thisArg
return _array_findIndex.call(_this, (x) =>
_isEqualityComparer ? thisArg.equals(x, arg) : x == arg
);

@@ -154,3 +157,3 @@ });

objectify,
joins,
// joins,
sortBy,

@@ -163,2 +166,4 @@ contains,

toObject,
_array_find,
_array_findIndex,
};

@@ -21,2 +21,4 @@ import test11 from "./all";

import test102 from "./Array.containsAny";
import test111 from "./Array.find";
import test121 from "./Array.findIndex";
import { TestRunner } from "@locustjs/test";

@@ -45,4 +47,6 @@

...test102,
...test111,
...test121
];
TestRunner.start(tests, true);

@@ -8,16 +8,14 @@ import { isObject } from "@locustjs/base";

function (expect) {
const obj = toObject(
const arr = [
["name", "ali"],
["age", 23],
[
["name", "ali"],
["age", 23],
"address",
[
"address",
[
["city", "Tehran"],
["code", "123"],
],
["city", "Tehran"],
["code", "123"],
],
],
"keyvalue"
);
];
const obj = toObject(arr, "keyvalue");

@@ -49,7 +47,5 @@ expect(obj).toBeDefined();

function (expect) {
const obj = toObject(["ali", 23, ["Tehran", "123"]], "values", [
"name",
"age",
["address", ["city", "code"]],
]);
const arr = ["ali", 23, ["Tehran", "123"]];
const schema = ["name", "age", ["address", ["city", "code"]]];
const obj = toObject(arr, "values", schema);

@@ -67,15 +63,14 @@ expect(obj).toBeDefined();

function (expect) {
const obj = toObject(
const arr = [
["name", "ali"],
["age", 23],
[
["name", "ali"],
["age", 23],
"address",
[
"address",
[
["city", "Tehran"],
["code", "123"],
],
["city", "Tehran"],
["code", "123"],
],
]
);
],
];
const obj = toObject(arr);

@@ -107,7 +102,5 @@ expect(obj).toBeDefined();

function (expect) {
const obj = toObject(["ali", 23, ["Tehran", "123"]], [
"name",
"age",
["address", ["city", "code"]],
]);
const values = ["ali", 23, ["Tehran", "123"]];
const schema = ["name", "age", ["address", ["city", "code"]]];
const obj = toObject(values, schema);

@@ -114,0 +107,0 @@ expect(obj).toBeDefined();