Socket
Socket
Sign inDemoInstall

array-sugar

Package Overview
Dependencies
0
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.1.1

165

array-sugar.js

@@ -40,34 +40,77 @@ (function (arr) {

var arrProt = arr.prototype;
var arrProt = arr.prototype;
var props = {
first: {
get: function() {
first: {
get: function() {
if (this.isEmpty) return undefined;
return this[0];
},
set: function(val) {
return this[0] = val;
}
},
last: {
get: function() {
},
set: function(val) {
return this[0] = val;
}
},
last: {
get: function() {
if (this.isEmpty) return undefined;
return this[this.length - 1];
},
set: function(val) {
return this[this.length - 1] = val;
}
},
isEmpty: {
get: function() {
return this.length === 0;
},
set: undefined
}
};
},
set: function(val) {
return this[this.length - 1] = val;
}
},
isEmpty: {
get: function() {
return this.length === 0;
},
set: undefined
},
unique: {
/**
* @returns {getUnique} function
*/
get: function(){
var self = this;
/**
*
* @returns {Array.<T>} with non unique items filtered out
*/
function getUnique() {
return self.filter(function(itm,i,a){
return i === a.indexOf(itm);
});
}
/**
* Inserts element to an array only if it is not present yet
* @param {Object} item to insert
*/
getUnique.insert = function(item) {
if (!self.contains(item)) {
self.push(item);
}
};
/**
* Merges only unique items from both arrays
* @param {Array} items if a si
*/
getUnique.merge = function(items) {
if (items && Object.prototype.toString.call(items) !== "[object Array]") {
throw new TypeError('Array was expected as a parameter for Array.unique.merge()');
}
for (var i = 0, count = items.length; i < count; i++) {
self.unique.insert(items[i]);
}
};
return getUnique;
}
}
};
var methods = {
/**
* traverses array and returns first element on which test function returns true
* @param test
* @param {Function<Boolean>} test
* @param {Boolean} fromEnd pass true if you want to traverse array from end to beginning

@@ -101,12 +144,10 @@ * @returns {*|null|undefined} an element of an array, or undefined when passed param is not a function

* @param {*} itemWith
* @returns {Number|false} index when item was replaced, false when not
* @returns {Number} index when item was replaced, -1 when not
*/
replace: function (toReplace, itemWith) {
var index = this.indexOf(toReplace);
if (~index) {
if (~index) { //Bitwise NOT operator-fast check if index is -1 or not
this[index] = itemWith;
return index;
} else {
return false;
}
return index;
},

@@ -134,36 +175,36 @@ /**

},
/**
* finds and removes the item from array
* @param item
* @returns {boolean} true when item was removed, else false
*/
remove: function (item) {
var index = this.indexOf(item);
if (index !== -1) {
this.splice(index, 1);
return true;
}
return false;
},
/**
* will erase the array
*/
clear: function () {
this.length = 0;
},
/**
* will return a copy of the array
*/
copy: function () {
return this.slice(0);
},
/**
* Rotates an array by given number of fields in given direction
* @param {Number} count
* @param {boolean} direction true for shifting array to the left
* @returns {Array}
*/
rotate: function (count,direction) {
return direction ? this.concat(this.splice(0,count)) : this.splice(this.length-count).concat(this);
}
/**
* finds and removes the item from array
* @param item
* @returns {boolean} true when item was removed, else false
*/
remove: function (item) {
var index = this.indexOf(item);
if (index !== -1) {
this.splice(index, 1);
return true;
}
return false;
},
/**
* will erase the array
*/
clear: function () {
this.length = 0;
},
/**
* will return a copy of the array
*/
copy: function () {
return this.slice(0);
},
/**
* Rotates an array by given number of fields in given direction
* @param {Number} count
* @param {boolean} direction true for shifting array to the left
* @returns {Array}
*/
rotate: function (count,direction) {
return direction ? this.concat(this.splice(0,count)) : this.splice(this.length-count).concat(this);
}
};

@@ -170,0 +211,0 @@

@@ -0,0 +0,0 @@ {

{
"name": "array-sugar",
"version": "1.0.0",
"version": "1.1.1",
"private": false,

@@ -5,0 +5,0 @@ "author": "capaj <capajj@gmail.com>",

@@ -0,0 +0,0 @@ array-sugar

require('./array-sugar');
var arr = [1,2,3,4,5];
var emptyArr = [1,2,3,4,5];
var forRemove = [1,2,3,4,5];
var arr = [1, 2, 3, 4, 5];
var emptyArr = [1, 2, 3, 4, 5];
var forRemove = [1, 2, 3, 4, 5];
module.exports = {
remove: function (test) {
test.equals(forRemove.remove(7), false);
test.equals(forRemove.remove(5), true);
test.equals(forRemove.last, 4);
test.equals(forRemove.length, 4);
test.done();
},
clear: function (test) {
test.equals(arr.isEmpty, false);
emptyArr.clear();
test.equals(emptyArr.length, 0);
test.equals(emptyArr.isEmpty, true);
test.done();
},
copy: function (test) {
var first = [1,2,3];
var second = first.copy();
test.equals(first.length, second.length);
second[1] = 4;
test.equals(first[1], 2);
test.equals(second[1], 4);
test.equals(first == second, false);
test.done();
},
lastFirst: function (test) {
test.equals(arr.first, 1);
test.equals(arr.last, 5);
arr.first = -1;
arr.last = 10;
test.equals(arr[0], -1);
test.equals(arr[arr.length-1], 10);
test.equals(typeof emptyArr.first, 'undefined');
test.equals(typeof emptyArr.last, 'undefined');
test.done();
},
contains: function (test) {
test.equals(arr.contains(3), true);
test.equals(arr.contains(6), false);
test.done();
},
range: function (test) {
test.equals(Array.range([],[]).length, 0);
test.equals(Array.range(10,5).length, 0);
test.equals(Array.range(5,10).length, 6);
test.done();
},
insert: function (test) {
var arr1 = ['a', 'c'];
arr1.insert(1, 'b');
test.equals(arr1.length, 3);
test.equals(arr1[1], 'b');
remove: function(test) {
test.equals(forRemove.remove(7), false);
test.equals(forRemove.remove(5), true);
test.equals(forRemove.last, 4);
test.equals(forRemove.length, 4);
test.done();
},
clear: function(test) {
test.equals(arr.isEmpty, false);
emptyArr.clear();
test.equals(emptyArr.length, 0);
test.equals(emptyArr.isEmpty, true);
test.done();
},
copy: function(test) {
var first = [1, 2, 3];
var second = first.copy();
test.equals(first.length, second.length);
second[1] = 4;
test.equals(first[1], 2);
test.equals(second[1], 4);
test.equals(first == second, false);
test.done();
},
lastFirst: function(test) {
test.equals(arr.first, 1);
test.equals(arr.last, 5);
arr.first = -1;
arr.last = 10;
test.equals(arr[0], -1);
test.equals(arr[arr.length - 1], 10);
test.equals(typeof emptyArr.first, 'undefined');
test.equals(typeof emptyArr.last, 'undefined');
test.done();
},
contains: function(test) {
test.equals(arr.contains(3), true);
test.equals(arr.contains(6), false);
test.done();
},
range: function(test) {
test.equals(Array.range([], []).length, 0);
test.equals(Array.range(10, 5).length, 0);
test.equals(Array.range(5, 10).length, 6);
test.done();
},
insert: function(test) {
var arr1 = ['a', 'c'];
arr1.insert(1, 'b');
test.equals(arr1.length, 3);
test.equals(arr1[1], 'b');
arr1.insert(arr1.length - 1, 'g', 'e');
test.equals(arr1.length, 5);
arr1.insert(arr1.length - 1);
test.equals(arr1.length, 5);
test.equals(arr1.insert(arr1.length-1), arr1);
arr1.insert(0, ['f','h']);
test.equals(arr1.length, 7);
test.done();
},
findOne: function (test) {
var first = {a:2};
var arr = [first, {b:3}, {c:6}, {a:2, c:3}];
var testFn = function (e) {
return e.a == 2;
};
test.equals(arr.findOne(testFn), first);
arr1.insert(arr1.length - 1, 'g', 'e');
test.equals(arr1.length, 5);
arr1.insert(arr1.length - 1);
test.equals(arr1.length, 5);
test.equals(arr1.insert(arr1.length - 1), arr1);
arr1.insert(0, ['f', 'h']);
test.equals(arr1.length, 7);
test.done();
},
unique: function (test){
var a=[1,5,1,6,4,5,2,5,4,3,1,2,6,6,3,3,2,4];
a = a.unique();
test.deepEqual(a, [1,5,6,4,2,3]);
test.done();
},
insertUnique: function (test) {
var arr1 = ['a', 'c'];
arr1.unique.insert('b');
test.equals(arr1.length, 3);
test.notEqual(arr.findOne(testFn, true), first);
test.equals(arr.findOne({}), undefined);
test.done();
},
replace: function (test) {
var arr = ['a', 'b', 'c'];
arr1.unique.insert('g');
test.equals(arr1.length, 4);
test.equals(arr.replace('aaa'), false);
test.equals(arr.replace('c', 'l'), 2);
test.equals(arr[2], 'l');
test.done();
},
max: function(test) {
var arr = [5,9,-10];
test.equals(Array.max(arr), 9);
test.done();
},
min: function(test) {
var arr = [5,9,-10];
test.equals(Array.min(arr), -10);
test.done();
arr1.unique.insert('g');
test.equals(arr1.length, 4);
}
arr1.unique.insert('b');
test.equals(arr1.length, 4);
test.done();
},
mergeUnique: function (test) {
var arr1 = ['a', 'b'];
arr1.unique.merge(['c','d']);
test.equals(arr1.length, 4);
test.throws(function() {
arr1.unique.merge('g');
});
test.equals(arr1.length, 4);
arr1.unique.merge([]);
test.equals(arr1.length, 4);
arr1.unique.merge(['e','b','f']);
test.equals(arr1.length, 6);
arr1.unique.merge(['a','c']);
test.equals(arr1.length, 6);
test.done();
},
findOne: function(test) {
var first = {a: 2};
var arr = [first, {b: 3}, {c: 6}, {a: 2, c: 3}];
var testFn = function(e) {
return e.a == 2;
};
test.equals(arr.findOne(testFn), first);
test.notEqual(arr.findOne(testFn, true), first);
test.equals(arr.findOne({}), undefined);
test.done();
},
replace: function(test) {
var arr = ['a', 'b', 'c'];
test.equals(arr.replace('aaa'), -1);
test.equals(arr.replace('c', 'l'), 2);
test.equals(arr[2], 'l');
test.done();
},
max: function(test) {
var arr = [5, 9, -10];
test.equals(Array.max(arr), 9);
test.done();
},
min: function(test) {
var arr = [5, 9, -10];
test.equals(Array.min(arr), -10);
test.done();
}
};

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

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc