prototypes
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -39,2 +39,35 @@ 'use strict'; | ||
/* | ||
* Returns a new sorted array of unique elements. Throws out null and undefined elements. | ||
*/ | ||
newArray.unique = function() { | ||
var result = [], index = -1, value = null; | ||
while(++index < this.length) { | ||
value = this[index]; | ||
if((value || value === 0) && !result.contains(value)) { | ||
result.push(value); | ||
} | ||
} | ||
return result.sort(); | ||
}; | ||
function testUnique(callback) { | ||
var array = []; | ||
testing.assertEquals(array.unique(), [], 'Array should be empty', callback); | ||
array = [4,1,6]; | ||
testing.assertEquals(array.unique(), [1,4,6], '', callback); | ||
array = [4,1,6,9,0,5,9,9,9,9,5,5,5,1,1,4]; | ||
testing.assertEquals(array.unique(), [0,1,4,5,6,9], '', callback); | ||
array = [null, null, 3, null, 3]; | ||
testing.assertEquals(array.unique(), [3], 'Null test', callback); | ||
array = [0, undefined, null]; | ||
testing.assertEquals(array.unique(), [0], 'Falsey test', callback); | ||
testing.success(callback); | ||
} | ||
/** | ||
@@ -72,32 +105,27 @@ * Remove an element from an array and return it if present; | ||
/** | ||
* Return a new array with only those elements that do not match the checker. Params: | ||
* - checker: a function that checks all elements, and only lets through | ||
* those for which the checker does _not_ return true. | ||
* Return the first element of an array, or undefined | ||
* for an empty array. | ||
*/ | ||
newArray.filterOut = function(checker) | ||
newArray.first = function() | ||
{ | ||
var result = []; | ||
this.forEach(function(element) | ||
{ | ||
if (!checker(element)) | ||
{ | ||
result.push(element); | ||
} | ||
}); | ||
return result; | ||
return this[0]; | ||
}; | ||
function testFilterOut(callback) | ||
/** | ||
* Return the first element of an array, or undefined | ||
* for an empty array. | ||
*/ | ||
newArray.last = function() | ||
{ | ||
var array = ['a', 'b', 'c', 'd1', 'd2']; | ||
var stringified = JSON.stringify(array); | ||
var removed = array.filterOut(function(element) | ||
{ | ||
return element.startsWith('d'); | ||
}); | ||
testing.assertEquals(array.length, 5, 'Original array shortened', callback); | ||
testing.assertEquals(JSON.stringify(array), stringified, 'Original array modified', callback); | ||
testing.assertEquals(removed.length, 3, 'Elements not removed', callback); | ||
testing.assert(!removed.contains('d1'), 'Wrong first element not filtered', callback); | ||
testing.assert(!removed.contains('d2'), 'Wrong second element not filtered', callback); | ||
return this[this.length - 1]; | ||
}; | ||
function testFirstLast(callback) | ||
{ | ||
var array = [1, 2, 3]; | ||
testing.assertEquals(array.first(), 1, 'Invalid first element', callback); | ||
testing.assertEquals(array.last(), 3, 'Invalid last element', callback); | ||
var empty = []; | ||
testing.assertEquals(empty.first(), undefined, 'Invalid first empty element', callback); | ||
testing.assertEquals(empty.last(), undefined, 'Invalid last empty element', callback); | ||
testing.success(callback); | ||
@@ -116,4 +144,5 @@ } | ||
testContains, | ||
testUnique, | ||
testRemove, | ||
testFilterOut, | ||
testFirstLast, | ||
]; | ||
@@ -120,0 +149,0 @@ testing.run(tests, callback); |
@@ -252,2 +252,21 @@ 'use strict'; | ||
/** | ||
* Format a string using the same convention as `util.format()`: | ||
* `%s` represents a string value, `%j` converts to JSON, and so on. | ||
*/ | ||
newString.format = function() | ||
{ | ||
var args = [this].concat(Array.prototype.slice.call(arguments, 0)); | ||
return util.format.apply(null, args); | ||
}; | ||
function testFormat(callback) | ||
{ | ||
var formatted = 'I am %s'.format('Bill'); | ||
testing.assertEquals(formatted, 'I am Bill', 'Invalid simple format', callback); | ||
formatted = 'Hi %s, %j'.format('a', {}); | ||
testing.assertEquals(formatted, 'Hi a, {}', 'Invalid complex format', callback); | ||
testing.success(callback); | ||
} | ||
// add new string functions to string prototype | ||
@@ -269,2 +288,3 @@ core.addProperties(String.prototype, newString); | ||
testPad, | ||
testFormat, | ||
]; | ||
@@ -271,0 +291,0 @@ testing.run(tests, callback); |
{ | ||
"name": "prototypes", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Some common prototypes for node.js: string.startsWith(), object.countProperties() and more. Facilities for functional programming with objects: object.forEach(), object.filter(). Functions are added safely using Object.defineProperty().", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/alexfernandez/prototypes", |
@@ -128,2 +128,13 @@ [![Build Status](https://secure.travis-ci.org/alexfernandez/prototypes.png)](http://travis-ci.org/alexfernandez/prototypes) | ||
### string.format() | ||
Format a string using the same convention as `util.format()`: | ||
`%s` represents a string value, `%j` converts to JSON, and so on. | ||
Example: | ||
``` | ||
'Hi %s, %j'.format('a', {}); | ||
\=> 'Hi a, {}' | ||
``` | ||
## Object Prototypes | ||
@@ -194,3 +205,3 @@ | ||
Does not modify the original object. | ||
For an array it is equivalent to `array.filter()`. | ||
Works also on arrays, equivalent to `array.filter()`. | ||
Example: | ||
@@ -212,3 +223,3 @@ | ||
Does not modify the original object. | ||
For an array it is equivalent to `array.filterOut()` added above. | ||
Works also on arrays. | ||
Example: | ||
@@ -271,7 +282,11 @@ | ||
### array.filterIn(checker) | ||
Inherited from `object.filterIn(checker)`, works also on arrays. | ||
Identical to `array.filter(checker)`. | ||
### array.filterOut(checker) | ||
Similar to `array.filter()` but reversed: returns an array | ||
whose members do _not_ return `true` with the checker. | ||
Example: | ||
Inherited from `object.filterIn(checker)`, works also on arrays. | ||
Similar to `array.filter()` but reversed. Example: | ||
@@ -283,5 +298,35 @@ ``` | ||
}); | ||
\=> true | ||
\=> ['a', 'b'] | ||
``` | ||
### array.unique() | ||
Returns a new sorted array of unique elements. | ||
Throws out null and undefined elements. Example: | ||
``` | ||
['c', 'a', 'b', 'c', 'b'].unique(); | ||
\=> ['a', 'b', 'c'] | ||
``` | ||
### array.first() | ||
Returns the first element of an array, or undefined | ||
for an empty array. Example: | ||
``` | ||
['a', 'b', 'c'].first(); | ||
\=> 'a' | ||
``` | ||
### array.last() | ||
Returns the last element of an array, or undefined | ||
for an empty array. Example: | ||
``` | ||
['a', 'b', 'c'].last(); | ||
\=> 'c' | ||
``` | ||
## Math-related Functions | ||
@@ -288,0 +333,0 @@ |
@@ -39,3 +39,3 @@ 'use strict'; | ||
}; | ||
var libs = [ 'core', 'string', 'math', 'object' ]; | ||
var libs = [ 'core', 'string', 'array', 'math', 'object' ]; | ||
libs.forEach(function(lib) | ||
@@ -42,0 +42,0 @@ { |
Sorry, the diff of this file is not supported yet
35969
952
406