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

array-x

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-x - npm Package Compare versions

Comparing version
2.0.9
to
3.0.0
+68
dist/types.d.ts
declare type Func = (currentValue: any, index?: number) => any
declare type Predicate = (currentValue: any, index?: number) => boolean
declare type Accumulator = (accumulatedValue: any, currentValue: any, index?: number) => any
declare var XArray : {
fromSize: (size?: number) => XArray<any>;
fromRange: (from: number, to: number) => XArray<any>;
}
declare interface XArray<T> extends Array<T> {
new<T>(...items) : XArray<T>;
toArray: () => any[];
distinct: () => XArray<T>;
accumulate: (initial: any, accum?: Accumulator) => any;
union: (array: Array<any>) => XArray<T>;
select: (func?: Func) => XArray<T>;
where: (func?: Predicate) => XArray<T>;
orderBy: (func?: Func) => XArray<T>;
orderByDesc: (func: Func) => XArray<T>;
firstOrDefault: (predicate?: Predicate) => T;
first: (predicate?: Predicate) => T;
lastOrDefault: (predicate?: Predicate) => T;
last: (predicate?: Predicate) => T;
all: (predicate?: Predicate) => boolean;
any: (predicate?: Predicate) => boolean;
take: (count: number) => XArray<T>;
skip: (count: number) => XArray<T>;
sum: (func?: Func) => number;
avg: (func?: Func) => number;
count: (func?: Func) => number;
min: (func?: Func) => number;
max: (func?: Func) => number;
/*
* Groups the array values into a dictionary of arrays.
* Grouping keys are determined by applying the <func> function arg to each element
*/
groupBy: (func) => any;
/*
* Groups this array into an dictionary of <keyCount> arrays,
* each containing the same number of elements, except possibly the last array,
* which may have less elements than the others
* if the length of this array is not divisible by <keyCount>
*/
groupByNumber(keyCount: number): any;
/*
* groups this array according to <key> generator function and then
* aggregates the resulting dictionary over <value> generator function
*/
groupAggregate(key, value, aggregation): any;
removeItem: (item: any) => XArray<T>;
removeAt: (index: number, count?: number) => XArray<T>;
insert: (index: number, ...items: any[]) => XArray<T>;
append: (items: any[]) => XArray<T>;
flatten: () => XArray<T>;
}
{
"compilerOptions": {
"baseUrl": ".",
"target": "es5",
"module": "commonjs",
"lib": [
"es7",
"es6",
"es5",
"es2015.promise",
"dom",
"scripthost",
"dom.iterable"
],
"noImplicitAny": false,
"suppressImplicitAnyIndexErrors": true,
"removeComments": false,
"preserveConstEnums": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noEmitHelpers": false,
"noEmitOnError": false,
"suppressExcessPropertyErrors": false
},
"exclude": [
"./dist",
"./node_modules"
]
}
+289
-272

@@ -1,276 +0,293 @@

"use strict";
var type = require("type-detect");
var clone = require("clone");
var associative = require("associative");
module.exports = function () {
Array.prototype.forAll = Array.prototype.forAll || function (func) {
for (var i = 0; i < this.length; i++) {
if (func) func(this[i], i);
}
};
Array.prototype.select = Array.prototype.select || function (func) {
var arr = [];
for (var i = 0; i < this.length; i++) {
arr.push(func ? func(this[i], i) : this[i]);
}return arr;
};
Array.prototype.distinct = Array.prototype.distinct || function () {
var u = {},
a = [];
for (var i = 0, l = this.length; i < l; ++i) {
if (u.hasOwnProperty(this[i])) continue;
a.push(this[i]);
u[this[i]] = 1;
}
return a;
};
Array.prototype.where = Array.prototype.where || function (func) {
var arr = [];
for (var i = 0; i < this.length; i++) {
if (!func || func(this[i], i)) arr.push(this[i]);
}return arr;
};
Array.prototype.firstOrDefault = Array.prototype.firstOrDefault || function (predicate) {
if (!predicate) return this.length > 0 ? this[0] : null;
for (var i = 0; i < this.length; i++) {
if (predicate(this[i]) === true) return this[i];
}return null;
};
Array.prototype.first = Array.prototype.first || function (predicate) {
var firstOrDefault = this.firstOrDefault(predicate);
if (firstOrDefault === null) throw new Error("First item not found");else return firstOrDefault;
};
Array.prototype.lastOrDefault = Array.prototype.lastOrDefault || function (predicate) {
if (!predicate) return this.length > 0 ? this[this.length - 1] : null;
for (var i = this.length - 1; i >= 0; i--) {
if (predicate(this[i]) === true) return this[i];
}return null;
};
Array.prototype.last = Array.prototype.last || function (predicate) {
var lastOrDefault = this.lastOrDefault(predicate);
if (lastOrDefault === null) throw new Error("Last item not found");else return lastOrDefault;
};
Array.prototype.all = Array.prototype.all || function (func) {
return this.where(func).length === this.length;
};
Array.prototype.any = Array.prototype.any || function (func) {
return this.firstOrDefault(func) !== null;
};
Array.prototype.accumulate = Array.prototype.accumulate || function (initial, func) {
// initial parameter is required
var accum = initial;
for (var i = 0; i < this.length; i++) {
accum = func ? func(accum, this[i]) : accum + this[i];
}return accum;
};
Array.prototype.take = Array.prototype.take || function (count) {
var arr = [];
for (var i = 0; i < count && i < this.length; i++) {
arr.push(this[i]);
}return arr;
};
Array.prototype.skip = Array.prototype.skip || function (count) {
var arr = [];
for (var i = count; i < this.length; i++) {
arr.push(this[i]);
}return arr;
};
Array.prototype.union = Array.prototype.union || function (array) {
var arr = [];
for (var i = 0; i < this.length; i++) {
arr.push(this[i]);
}if (type(array) === "array") for (var k = 0; k < array.length; k++) {
arr.push(array[k]);
}return arr;
};
Array.prototype.orderBy = Array.prototype.orderBy || function (func) {
return this.select().sort(function (a, b) {
var objA = func ? func(a) : a;
var objB = func ? func(b) : b;
var comparison = null;
if (objA && objB) {
if (type(objA) === "number") comparison = objA - objB;else if (type(objA) === "date") comparison = objA.getTime() - objB.getTime();else comparison = objA.toString().localeCompare(objB.toString());
} else {
if (!objA && !objB) comparison = 0;else if (!objA) comparison = -1;else comparison = 1;
module.exports = (function () {
var type = require("type-detect");
var isNil = require("is-nil");
var clone = require("clone");
var associative = require("associative");
var XArray = (function () {
function XArray() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return comparison;
});
};
Array.prototype.orderByDesc = Array.prototype.orderByDesc || function (func) {
return this.orderBy(func).reverse();
};
Array.prototype.sum = Array.prototype.sum || function (func) {
var sum = 0;
for (var i = 0; i < this.length; i++) {
sum += func ? func(this[i]) : 0;
}return sum;
};
Array.prototype.count = Array.prototype.count || function (func) {
var count = 0;
for (var i = 0; i < this.length; i++) {
if (true === (func ? func(this[i]) : this[i])) count++;
}return count;
};
Array.prototype.avg = Array.prototype.avg || function (func) {
var count = this.count(func);
if (count === 0) return 0;else return this.sum(func) / count;
};
Array.prototype.max = Array.prototype.max || function (func) {
var max = Number.NEGATIVE_INFINITY;
for (var i = 0; i < this.length; i++) {
var value = func ? func(this[i]) : this[i];
if (value > max) max = value;
var Arr = new Array(0);
args.forEach(function (arg) {
Arr.push(arg);
});
Arr["forAll"] = function (func) {
return this.forEach(func);
};
Arr["distinct"] = function () {
var arr = [];
for (var i = 0; i < this.length; i++) {
if (arr.indexOf(this[i]) < 0)
arr.push(this[i]);
}
return new XArray(arr);
};
Arr["accumulate"] = function (initial, accum) {
var val = initial; // initial parameter is required
for (var i = 0; i < this.length; i++)
val = accum ? accum(accum, this[i]) : (val + this[i]);
return val;
};
Arr["union"] = function (array) {
var arr = [];
for (var i = 0; i < this.length; i++)
arr.push(this[i]);
if (type(array) === "array")
for (var k = 0; k < array.length; k++)
arr.push(array[k]);
return new XArray(arr);
};
Arr["select"] = function (func) {
var arr = [];
for (var i = 0; i < this.length; i++)
arr.push(func ? func(this[i], i) : this[i]);
return new XArray(arr);
};
Arr["where"] = function (func) {
var arr = [];
for (var i = 0; i < this.length; i++)
if (isNil(func) || func(this[i], i) === true)
arr.push(this[i]);
return new XArray(arr);
};
Arr["orderBy"] = function (func) {
return new XArray(this
.select()
.sort(function (a, b) {
var objA = func ? func(a) : a;
var objB = func ? func(b) : b;
var comparison = null;
if (objA && objB) {
if (type(objA) === "number")
comparison = objA - objB;
else if (type(objA) === "date")
comparison = objA.getTime() - objB.getTime();
else
comparison = objA.toString().localeCompare(objB.toString());
}
else {
if (!objA && !objB)
comparison = 0;
else if (!objA)
comparison = -1;
else
comparison = 1;
}
return comparison;
}));
};
Arr["orderByDesc"] = function (func) {
return new XArray(this.orderBy(func).reverse());
};
Arr["firstOrDefault"] = function (predicate) {
if (!predicate)
return this.length > 0 ? this[0] : null;
for (var i = 0; i < this.length; i++)
if (predicate(this[i]) === true)
return this[i];
return null;
};
Arr["first"] = function (predicate) {
var firstOrDefault = this.firstOrDefault(predicate);
if (isNil(firstOrDefault))
throw new Error("First item not found");
else
return firstOrDefault;
};
Arr["lastOrDefault"] = function (predicate) {
if (isNil(predicate))
return this.length > 0 ? this[this.length - 1] : null;
for (var i = (this.length - 1); i >= 0; i--)
if (predicate(this[i]) === true)
return this[i];
return null;
};
Arr["last"] = function (predicate) {
var lastOrDefault = this.lastOrDefault(predicate);
if (isNil(lastOrDefault))
throw new Error("Last item not found");
else
return lastOrDefault;
};
Arr["all"] = function (predicate) {
return this.where(predicate).length === this.length;
};
Arr["any"] = function (predicate) {
return this.firstOrDefault(predicate) !== null;
};
Arr["take"] = function (count) {
var arr = [];
for (var i = 0; (i < count && i < this.length); i++)
arr.push(this[i]);
return new XArray(arr);
};
Arr["skip"] = function (count) {
var arr = [];
for (var i = count; i < this.length; i++)
arr.push(this[i]);
return new XArray(arr);
};
Arr["sum"] = function (func) {
var sum = 0;
for (var i = 0; i < this.length; i++)
sum += (func ? func(this[i]) : 0);
return sum;
};
Arr["avg"] = function (func) {
var count = this.count(func);
if (count === 0)
return 0;
else
return this.sum(func) / count;
};
Arr["count"] = function (func) {
var count = 0;
for (var i = 0; i < this.length; i++)
if (true === (func ? func(this[i]) : this[i]))
count++;
return count;
};
Arr["min"] = function (func) {
var min = Number.POSITIVE_INFINITY;
for (var i = 0; i < this.length; i++) {
var value = (func ? func(this[i]) : this[i]);
if (value < min)
min = value;
}
return min;
};
Arr["max"] = function (func) {
var max = Number.NEGATIVE_INFINITY;
for (var i = 0; i < this.length; i++) {
var value = (func ? func(this[i]) : this[i]);
if (value > max)
max = value;
}
return max;
};
/*
* Groups the array values into a dictionary of arrays.
* Grouping keys are determined by applying the <func> function arg to each element
*/
Arr["groupBy"] = function (func) {
var groups = new associative.Dictionary();
for (var i = 0; i < this.length; i++) {
var key = func(this[i]);
if (!groups.hasKey(key))
groups.put(key, []);
groups.get(key).push(this[i]);
}
return groups;
};
/*
* Groups this array into an dictionary of <keyCount> arrays,
* each containing the same number of elements, except possibly the last array,
* which may have less elements than the others
* if the length of this array is not divisible by <keyCount>
*/
Arr["groupByNumber"] = function (keyCount) {
var groups = new associative.Dictionary();
var groupLength = Math.floor(this.length / keyCount);
var remainder = this.length % this.count();
for (var i = 0; i < keyCount; i++) {
if (!groups.hasKey(i))
groups.put(i, []);
groups.get(i).append(this.toArray().slice(i * groupLength, (i + 1) * groupLength));
}
return groups;
};
/*
* groups this array according to <key> generator function and then
* aggregates the resulting dictionary over <value> generator function
*/
Arr["groupAggregate"] = function (key, value, aggregation) {
// if key is passed as generator function, use it, else create generator function that assumes key is a property name.
// If key is null/missing, the string representation of an object is used as the key.
var keyGenerator = type(key) === "function"
? key
: function (datum) { return key
? datum[key.toString()]
: datum.toString(); };
//Same for value
var valueGenerator = type(value) === "function"
? value
: function (datum) { return value
? datum[value.toString()]
: datum.toString(); };
return this
.groupBy(keyGenerator)
.select(function (groupData) {
return groupData[aggregation](valueGenerator);
});
};
Arr["removeItem"] = function (item) {
var arr = clone(this);
var _index = arr.indexOf(item);
while (_index >= 0) {
arr.splice(_index, 1);
_index = arr.indexOf(item);
}
return new XArray(arr);
};
Arr["removeAt"] = function (index, count) {
var arr = clone(this);
arr.splice(index, count);
return new XArray(arr);
};
Arr["insert"] = function (index) {
var items = [];
for (var _i = 1; _i < arguments.length; _i++) {
items[_i - 1] = arguments[_i];
}
var arr = clone(this);
arr.splice.apply(arr, [index, 0].concat(items));
return new XArray(arr);
};
Arr["append"] = function (items) {
var arr = clone(this);
arr.splice.apply(arr, [this.length, 0].concat(items));
return new XArray(arr);
};
Arr["flatten"] = function () {
var mutable = false;
var array = this;
var toString = Object.prototype.toString;
var arrayTypeStr = '[object Array]';
var result = [];
var nodes = (mutable && array) || array.slice();
var node;
if (!array.length) {
return new XArray(result);
}
node = nodes.pop();
do {
if (toString.call(node) === arrayTypeStr) {
nodes.push.apply(nodes, node);
}
else {
result.push(node);
}
} while (nodes.length && (node = nodes.pop()) !== undefined);
result.reverse(); // we reverse result to restore the original order
return new XArray(result);
};
return Arr;
}
return max;
};
Array.prototype.min = Array.prototype.min || function (func) {
var min = Number.POSITIVE_INFINITY;
for (var i = 0; i < this.length; i++) {
var value = func ? func(this[i]) : this[i];
if (value < max) min = value;
}
return min;
};
// Groups the array values into a dictionary of arrays.
// The grouping keys are determined by applying the <func> function argument to each element
Array.prototype.groupBy = Array.prototype.groupBy || function (func) {
var groups = new associative.Dictionary();
for (var i = 0; i < this.length; i++) {
var key = func(this[i]);
if (!groups.hasKey(key)) groups.put(key, []);
groups.get(key).push(this[i]);
}
return groups;
};
// Groups this array into an dictionary of <keyCount> arrays,
// each containing the same number of elements, except possibly the last array,
// which may have less elements than the others
// if the length of this array is not divisible by <keyCount>
Array.prototype.groupByNumber = Array.prototype.groupByNumber || function (keyCount) {
var groups = new associative.Dictionary();
var groupLength = Math.floor(this.length / keyCount);
var remainder = this.length % count;
for (var i = 0; i < keyCount; i++) {
if (!groups.hasKey(i)) groups.put(i, []);
groups.get(i).append(this.slice(i * groupLength, (i + 1) * groupLength));
}
return groups;
};
// groups this array according to <key> generator function and then
// aggregates the resulting dictionary over <value> generator function
Array.prototype.groupAggregate = Array.prototype.groupAggregate || function (key, value, aggregation) {
// if key is passed as generator function, use it, else create generator function that assumes key is a property name.
// If key is null/missing, the string representation of an object is used as the key.
var keyGenerator = type(key) === "function" ? key : function (datum) {
return key ? datum[key.toString()] : datum.toString();
XArray.fromLength = function (size) {
if (size === void 0) { size = 0; }
return new XArray(new Array(size));
};
//Same for value
var valueGenerator = type(value) === "function" ? value : function (datum) {
return value ? datum[value.toString()] : datum.toString();
XArray.fromRange = function (from, to) {
var _arr = [];
var sgn = to > from ? 1 : -1;
for (var idx = from; idx != to; idx += sgn)
_arr.push(idx);
return new XArray(_arr);
};
return this.groupBy(keyGenerator).select(function (groupData) {
return (// e.g., groupData.sum(valueGenerator)
groupData[aggregation](valueGenerator)
);
});
};
Array.prototype.removeItem = Array.prototype.removeItem || function (item) {
var arr = clone(this);
var _index = arr.indexOf(item);
while (_index >= 0) {
arr.splice(_index, 1, 0);
_index = arr.indexOf(item);
}
return arr;
};
Array.prototype.removeAt = Array.prototype.removeAt || function (index, count) {
var arr = clone(this);
arr.splice(index, count, 0);
return arr;
};
Array.prototype.insert = Array.prototype.insert || function (index) {
var arr = clone(this);
for (var _len = arguments.length, items = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
items[_key - 1] = arguments[_key];
}
arr.splice(index, 0, items);
return arr;
};
Array.prototype.append = Array.prototype.append || function (items) {
var arr = clone(this);
arr.splice(this.length, 0, items);
return arr;
};
Array.prototype.flatten = Array.prototype.flatten || function () {
var mutable = false;
var array = this;
var toString = Object.prototype.toString;
var arrayTypeStr = '[object Array]';
var result = [];
var nodes = mutable && array || array.slice();
var node;
if (!array.length) {
return result;
}
node = nodes.pop();
do {
if (toString.call(node) === arrayTypeStr) {
nodes.push.apply(nodes, node);
} else {
result.push(node);
}
} while (nodes.length && (node = nodes.pop()) !== undefined);
result.reverse(); // we reverse result to restore the original order
return result;
};
return {};
}();
return XArray;
}());
;
return XArray;
})();
{
"name": "array-x",
"version": "2.0.9",
"version": "3.0.0",
"description": "Functional extension methods (inspired by LINQ .NET) on JavaScript arrays",
"types": "./dist/types.d.ts",
"main": "./dist/index.js",
"scripts": {
"build": "gulp compile"
"build": "tsc --outDir ./dist && cpy ./src/types.d.ts ./dist"
},

@@ -24,12 +25,5 @@ "keywords": [

"devDependencies": {
"babel-core": "^6.14.0",
"babel-plugin-transform-class-properties": "^6.10.2",
"babel-plugin-transform-decorators": "^6.8.0",
"babel-plugin-transform-eval": "^6.8.0",
"babel-plugin-transform-object-rest-spread": "^6.8.0",
"babel-preset-es2015": "^6.14.0",
"console-x": "^0.1.8",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"gulp-rename": "^1.2.2"
"@types/node": "^7.0.5",
"cpy-cli": "^1.0.1",
"typescript": "^2.2.1"
},

@@ -39,4 +33,5 @@ "dependencies": {

"clone": "^1.0.2",
"is-nil": "^1.0.1",
"type-detect": "^2.0.2"
}
}
+62
-50
# array-x.js
Functional extension methods (inspired by LINQ .NET) on JavaScript arrays
Functional methods (inspired by LINQ) on JavaScript arrays

@@ -7,6 +7,6 @@

```
require("array-x");
var xArray: XArray<any> = require("array-x");
var arr = [
new Date(),
var arr = new xArray<any>(
new Date("March 21, 2020"),
/abc[.]+/g,

@@ -39,3 +39,3 @@ "a", "b", "c",

"last"
];
);

@@ -47,74 +47,86 @@ console.log("first(): " + arr.first());

console.log("select(x=>x.length): " + arr.select(x => x.length));
console.log("flatten(): " + arr.flatten());
console.log("append(): " + arr.append(["a", "b", "c"]));
```
Output:
```
first(): Wed Sep 14 2016 12:33:57 GMT-0700 (PDT)
first(): Sat Mar 21 2020 00:00:00 GMT-0700 (Pacific Daylight Time)
last(): last
skip(2): a,b,c,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,8,9,10,last
where(x=>x>3):Wed Sep 14 2016 12:33:57 GMT-0700 (PDT),4,5,6,7
where(x=>x>3):Sat Mar 21 2020 00:00:00 GMT-0700 (Pacific Daylight Time),4,5,6,7
select(x=>x.length): ,,1,1,1,,,,,,,,2,2,3,4
flatten(): Sat Mar 21 2020 00:00:00 GMT-0700 (Pacific Daylight Time),/abc[.]+/g,a,b,c,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,8,9,10,last
append(): Sat Mar 21 2020 00:00:00 GMT-0700 (Pacific Daylight Time),/abc[.]+/g,a,b,c,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,8,9,10,last,a,b,c
```
## API
Array.prototype.forAll(function);
Array.prototype.select(function);
declare var XArray : {
fromSize: (size?: number) => XArray<any>;
fromRange: (from: number, to: number) => XArray<any>;
}
Array.prototype.distinct();
declare interface XArray<T> extends Array<T> {
new<T>(...items) : XArray<T>;
Array.prototype.where(function);
toArray: () => any[];
distinct: () => XArray<T>;
accumulate: (initial: any, accum?: Accumulator) => any;
union: (array: Array<any>) => XArray<T>;
Array.prototype.all(function);
select: (func?: Func) => XArray<T>;
where: (func?: Predicate) => XArray<T>;
Array.prototype.any(function);
orderBy: (func?: Func) => XArray<T>;
orderByDesc: (func: Func) => XArray<T>;
Array.prototype.accumulate(initial, function);
firstOrDefault: (predicate?: Predicate) => T;
first: (predicate?: Predicate) => T;
Array.prototype.firstOrDefault(function);
lastOrDefault: (predicate?: Predicate) => T;
last: (predicate?: Predicate) => T;
Array.prototype.first(function);
all: (predicate?: Predicate) => boolean;
any: (predicate?: Predicate) => boolean;
Array.prototype.lastOrDefault(function);
take: (count: number) => XArray<T>;
skip: (count: number) => XArray<T>;
Array.prototype.last(function);
sum: (func?: Func) => number;
avg: (func?: Func) => number;
count: (func?: Func) => number;
min: (func?: Func) => number;
max: (func?: Func) => number;
Array.prototype.take(count)
/*
* Groups the array values into a dictionary of arrays.
* Grouping keys are determined by applying the <func> function arg to each element
*/
groupBy: (func) => any;
Array.prototype.skip(count)
/*
* Groups this array into an dictionary of <keyCount> arrays,
* each containing the same number of elements, except possibly the last array,
* which may have less elements than the others
* if the length of this array is not divisible by <keyCount>
*/
groupByNumber(keyCount: number): any;
Array.prototype.union(array);
/*
* groups this array according to <key> generator function and then
* aggregates the resulting dictionary over <value> generator function
*/
groupAggregate(key, value, aggregation): any;
Array.prototype.orderBy(function);
removeItem: (item: any) => XArray<T>;
removeAt: (index: number, count?: number) => XArray<T>;
insert: (index: number, ...items: any[]) => XArray<T>;
append: (items: any[]) => XArray<T>;
Array.prototype.orderByDesc(function);
flatten: () => XArray<T>;
}
Array.prototype.sum(function);
Array.prototype.count(function);
Array.prototype.avg(function);
Array.prototype.max(function);
Array.prototype.min(function);
Array.prototype.groupBy(function);
Array.prototype.groupByNumber(keyCount);
Array.prototype.groupAggregate(key, value, aggregation);
Array.prototype.removeItem(item);
Array.prototype.removeAt(index);
Array.prototype.insert(index, item);
## Install
npm install array-x --save