prototypes
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -10,3 +10,3 @@ 'use strict'; | ||
var testing = require('testing'); | ||
var object = require('./object.js'); | ||
var core = require('./core.js'); | ||
@@ -91,3 +91,3 @@ // globals | ||
// add functions to number as properties | ||
object.addProperties(Number.prototype, newNumber); | ||
core.addProperties(Number.prototype, newNumber); | ||
@@ -94,0 +94,0 @@ /** |
@@ -10,2 +10,4 @@ 'use strict'; | ||
// requires | ||
require('./string.js'); | ||
var core = require('./core.js'); | ||
var testing = require('testing'); | ||
@@ -17,10 +19,45 @@ | ||
/** | ||
* Count the number of properties in an object. | ||
* Count the number of properties in an object. Params: | ||
* - filter: can be a string that has to be contained in every key, | ||
* or a function which will be used as a filter to pass keys. | ||
*/ | ||
newObject.countProperties = function() | ||
newObject.countProperties = function(filter) | ||
{ | ||
return Object.keys(this).length; | ||
if (!filter) | ||
{ | ||
return Object.keys(this).length; | ||
} | ||
var filterer = getFilterer(filter); | ||
var count = 0; | ||
for (var key in this) | ||
{ | ||
if (!this.hasOwnProperty(key)) | ||
{ | ||
continue; | ||
} | ||
if (filterer(key)) | ||
{ | ||
count++; | ||
} | ||
} | ||
return count; | ||
}; | ||
/** | ||
* Get a function to filter keys. | ||
*/ | ||
function getFilterer(filter) | ||
{ | ||
if (typeof filter == 'function') | ||
{ | ||
return filter; | ||
} | ||
return function(key) | ||
{ | ||
return key.contains(filter); | ||
}; | ||
} | ||
/** | ||
* Test counting properties. | ||
@@ -31,5 +68,9 @@ */ | ||
var object = { | ||
a: 'a', | ||
hello: 'a', | ||
}; | ||
testing.assertEquals(object.countProperties(), 1, 'Invalid property count', callback); | ||
testing.assertEquals(object.countProperties('ll'), 1, 'Invalid filtered property count', callback); | ||
testing.assertEquals(object.countProperties('pk'), 0, 'Invalid non-filtered property count', callback); | ||
testing.assertEquals(object.countProperties(getFilter(5)), 1, 'Invalid length property count', callback); | ||
testing.assertEquals(object.countProperties(getFilter(3)), 0, 'Invalid length property count', callback); | ||
testing.success(callback); | ||
@@ -39,2 +80,13 @@ } | ||
/** | ||
* Get a filter for keys with the given length. | ||
*/ | ||
function getFilter(length) | ||
{ | ||
return function(key) | ||
{ | ||
return key.length == length; | ||
}; | ||
} | ||
/** | ||
* Overwrite properties in the original with the given object. | ||
@@ -74,17 +126,4 @@ */ | ||
/** | ||
* Define new prototype functions as properties. | ||
*/ | ||
exports.addProperties = function(original, extension) | ||
{ | ||
for (var key in extension) | ||
{ | ||
Object.defineProperty(original, key, { | ||
value: extension[key], | ||
}); | ||
} | ||
}; | ||
// add new object functions as properties | ||
exports.addProperties(Object.prototype, newObject); | ||
core.addProperties(Object.prototype, newObject); | ||
@@ -91,0 +130,0 @@ /** |
@@ -10,3 +10,3 @@ 'use strict'; | ||
var testing = require('testing'); | ||
var object = require('./object.js'); | ||
var core = require('./core.js'); | ||
@@ -189,3 +189,3 @@ // globals | ||
// add new string functions to string prototype | ||
object.addProperties(String.prototype, newString); | ||
core.addProperties(String.prototype, newString); | ||
@@ -192,0 +192,0 @@ /** |
{ | ||
"name": "prototypes", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Some common prototypes for node.js: string.startsWith(), object.countProperties() and more. Functions are added using Object.defineProperty() to avoid polluting new objects.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/alexfernandez/prototypes", |
@@ -122,6 +122,6 @@ [![Build Status](https://secure.travis-ci.org/alexfernandez/prototypes.png)](http://travis-ci.org/alexfernandez/prototypes) | ||
### object.countProperties(object) | ||
### object.countProperties(filter) | ||
Count the number of properties in an object. | ||
Does not count inherited properties: uses hasOwnProperty(). | ||
Does not count inherited properties: uses Object.keys(). | ||
Example: | ||
@@ -132,2 +132,10 @@ | ||
You can also pass a string or a function as a filter: | ||
{hello: 'a'}.countProperties('ll'); | ||
\=> 1 | ||
{hello: 'a'}.countProperties(function(key) { return key.length == 5 }); | ||
\=> 1 | ||
### object.overwriteWith(overwriter) | ||
@@ -134,0 +142,0 @@ |
@@ -39,3 +39,3 @@ 'use strict'; | ||
}; | ||
var libs = [ 'string', 'math', 'object' ]; | ||
var libs = [ 'core', 'string', 'math', 'object' ]; | ||
libs.forEach(function(lib) | ||
@@ -42,0 +42,0 @@ { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19146
12
516
186