array-sugar
Advanced tools
Comparing version
{ | ||
"name": "array-sugar", | ||
"version": "0.0.7", | ||
"main": "index.js", | ||
"version": "0.2.1", | ||
"ignore": [ | ||
"**/.*", | ||
"node_modules", | ||
".idea", | ||
"components", | ||
@@ -9,0 +9,0 @@ "test.js" |
{ | ||
"name": "array-sugar", | ||
"version": "0.0.7", | ||
"version": "0.2.1", | ||
"private": false, | ||
"author": "capaj <capajj@gmail.com>", | ||
"description": "adds last, first, contains, isEmpty and empty to array utilizing Object.defineProperty", | ||
"description": "adds last, first, remove, contains, isEmpty and empty to array utilizing Object.defineProperty", | ||
"repository": { | ||
@@ -11,2 +11,3 @@ "type": "git", | ||
}, | ||
"main": "./array-sugar", | ||
"engines": { | ||
@@ -18,3 +19,5 @@ "node": ">=0.8" | ||
"array-methods", | ||
"extend" | ||
"extend", | ||
"sugar", | ||
"syntactic sugar" | ||
], | ||
@@ -21,0 +24,0 @@ "license": "MIT", |
@@ -5,10 +5,13 @@ array-sugar | ||
Tired of not having a last property on array in Javascript? Tired of not having contains method? | ||
This little script solves those by adding a bit of sugar: | ||
var array = [1, 2, 3]; | ||
array.contains(o) instead array.indexOf(o) != -1 | ||
array.clear() instead array.length = 0 | ||
array.isEmpty instead array.length == 0 | ||
array.first instead array[0] | ||
array.last instead array[array.length-1] | ||
This little script solves those by adding a bit of sugar allowing you to do: | ||
```javascript | ||
Array.range(1,3) instead [1, 2, 3]; | ||
array.contains(o) instead array.indexOf(o) != -1 | ||
array.remove(o) instead array.splice(array.indexOf(o), 1) //remove returns true when o was removed | ||
array.clear() instead array.length = 0 | ||
array.copy() instead array.slice(0) | ||
array.isEmpty instead array.length == 0 | ||
array.first instead array[0] | ||
array.last instead array[array.length-1] | ||
``` | ||
Usable in any environment that supports Object.defineProperty(oldest would probably be IE9). | ||
@@ -23,2 +26,7 @@ | ||
#known incompatibilities | ||
Angular ngSanitize module in version 1.2.0-rc2 in it's method assigns to arr.last and this collides with our 'last' getter. | ||
So beware, I have created a pull request so that ngSanitize doesn't break, but who knows when it is going to be merged. For now, use ngSanitize from my fork: | ||
[compatible ngSanitize](https://github.com/capaj/angular.js/blob/master/src/ngSanitize/sanitize.js) | ||
Missing any sugar? Please do submit a pull or feature request. |
49
test.js
@@ -1,7 +0,15 @@ | ||
require('./index'); | ||
require('./array-sugar'); | ||
var arr = [1,2,3,4,5]; | ||
var emptyArr = [1,2,3,4,5]; | ||
var forRemove = [1,2,3,4,5]; | ||
module.exports = { | ||
empty: function (test) { | ||
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); | ||
@@ -13,2 +21,12 @@ emptyArr.clear(); | ||
}, | ||
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) { | ||
@@ -21,4 +39,4 @@ test.equals(arr.first, 1); | ||
test.equals(arr[arr.length-1], 10); | ||
test.equals(emptyArr.first, null); | ||
test.equals(emptyArr.last, null); | ||
test.equals(typeof emptyArr.first, 'undefined'); | ||
test.equals(typeof emptyArr.last, 'undefined'); | ||
test.done(); | ||
@@ -30,3 +48,24 @@ }, | ||
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(); | ||
} | ||
}; |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
191
94.9%31
34.78%39010
-8.08%1
Infinity%