Socket
Socket
Sign inDemoInstall

prototypes

Package Overview
Dependencies
1
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.4 to 0.2.6

4

lib/core.js

@@ -19,2 +19,6 @@ 'use strict';

{
if (original.hasOwnProperty(key))
{
continue;
}
Object.defineProperty(original, key, {

@@ -21,0 +25,0 @@ value: extension[key],

@@ -60,5 +60,2 @@ 'use strict';

/**
* Test counting properties.
*/
function testCountProperties(callback)

@@ -104,5 +101,2 @@ {

/**
* Test that overwrite object works.
*/
function testOverwriteObject(callback)

@@ -134,2 +128,116 @@ {

/**
* Return a new object that contains the properties of this and the parameter object.
*/
newObject.concat = function(object)
{
var result = {};
result.overwriteWith(this);
result.overwriteWith(object);
return result;
};
function testConcat(callback)
{
var first = {
a: 'a',
b: 'b',
};
var second = {
c: 'b2',
d: {d: 5},
};
var result = first.concat(second);
testing.assertEquals(result.countProperties(), first.countProperties() + second.countProperties(), 'Invalid concat length', callback);
for (var key in first)
{
testing.assertEquals(result[key], first[key], 'Invalid first key', callback);
}
for (key in second)
{
testing.assertEquals(result[key], second[key], 'Invalid second key', callback);
}
testing.success(callback);
}
/**
* Call the callback for each property of the object.
*/
newObject.forEach = function(callback)
{
for (var key in this)
{
callback(this[key], key, this);
}
};
function testForEach(callback)
{
var object = {
a: 1,
b: 2,
};
var count = 0;
var total = 0;
var keys = [];
object.forEach(function(value, key)
{
count += 1;
total += value;
keys.push(key);
});
testing.assertEquals(count, 2, 'Invalid count', callback);
testing.assertEquals(total, 3, 'Invalid total', callback);
testing.assertEquals(keys, ['a', 'b'], 'Invalid keys', callback);
testing.success(callback);
}
/**
* Return a new object with just the properties that pass the callback.
*/
newObject.filter = function(callback)
{
var result = {};
for (var key in this)
{
var value = this[key];
if (callback(value))
{
result[key] = value;
}
}
return result;
};
function testFilter(callback)
{
var object = {
a: 1,
b: 2,
c: 3,
};
var filtered = object.filter(function(value)
{
return value < 3;
});
testing.assertEquals(filtered.countProperties(), 2, 'Invalid <3 count', callback);
testing.assertEquals(filtered.a, 1, 'Invalid a in filtered', callback);
testing.assertEquals(filtered.b, 2, 'Invalid b in filtered', callback);
filtered = object.filter(function(value)
{
return value > 2;
});
testing.assertEquals(filtered.countProperties(), 1, 'Invalid >3 count', callback);
testing.assertEquals(filtered.c, 3, 'Invalid c in filtered', callback);
testing.success(callback);
}
/**
* Find out if the object is an array.
*/
newObject.isArray = function()
{
return Array.isArray(this);
};
// add new object functions as properties

@@ -146,2 +254,5 @@ core.addProperties(Object.prototype, newObject);

testOverwriteObject,
testConcat,
testFilter,
testForEach,
];

@@ -148,0 +259,0 @@ testing.run(tests, callback);

6

package.json
{
"name": "prototypes",
"version": "0.2.4",
"description": "Some common prototypes for node.js: string.startsWith(), object.countProperties() and more. Functions are added using Object.defineProperty() to avoid polluting new objects.",
"version": "0.2.6",
"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().",
"homepage": "https://github.com/alexfernandez/prototypes",

@@ -16,3 +16,3 @@ "contributors": ["Alex Fernández <alexfernandeznpm@gmail.com>"],

},
"keywords" : ["prototypes", "prototype", "string", "object"],
"keywords" : ["prototypes", "prototype", "string", "object", "functional programming", "functional"],
"engines": {

@@ -19,0 +19,0 @@ "node": "*"

@@ -5,6 +5,9 @@ [![Build Status](https://secure.travis-ci.org/alexfernandez/prototypes.png)](http://travis-ci.org/alexfernandez/prototypes)

Some common prototypes for node.js: string.startsWith(),
object.countProperties() and more.
Functions are added using Object.defineProperty() to avoid polluting new objects.
Some common prototypes for node.js: `string.startsWith()`,
`object.countProperties()` and more.
Functions are added using `Object.defineProperty()` to avoid polluting new objects.
Includes nice facilities for functional programming with objects:
`object.forEach()`, `object.filter()` and so on.
## Installation

@@ -20,3 +23,3 @@

This package adds some useful prototypes to String.
This package adds some useful prototypes to `String`, `Object` and `Array`.
To use in your package, you just have to require prototypes:

@@ -26,18 +29,10 @@

You do not need to assign the result to any variable, and in fact JSHint
(and similar code checkers) may complain about an unused variable if you
do this:
There is no need to assign the result to any variable, since the prototypes
are added automatically. It may in fact result in a warning in JSHint
or similar code checkers.
var prototypes = require('prototypes');
Special care has been taken to avoid nasty interactions with other libraries:
the new function prototypes don't appear in enumerations and can be overwritten
in your own code.
This last form is only required if you use any of the exported functions,
which should be seldom the case.
### License
This package is published under the MIT license.
You can integrate it in any commercial, closed software and/or make changes to the code with complete liberty.
If you send your changes back to the main repo we will be grateful,
but it is by no means required.
## String Prototypes

@@ -170,2 +165,55 @@

### object.concat(otherObject)
Return a new object that includes properties of the object
and the other object. Does not modify the original object.
Example:
{a: 'a'}.concat({b: 'b'});
\=> {a: 'a', b: 'b'}
### object.forEach(callback)
Call the callback for every value of the object.
Similar to `array.forEach()`, the callback will receive three parameters:
value, key and the object itself.
Example:
```
{a: 1, b: 2}.forEach(function(value, key)
{
console.log(key + ': ' + value);
});
\=> a: 1
b: 2
```
### object.filter(callback)
Return a new object that only includes those properties of the object
that return true for the given callback, i.e.:
`callback(value) == true`.
Does not modify the original object.
Example:
```
{a: 1, b: 2}.forEach(function(value)
{
return value > 1;
});
\=> {b: 2}
```
### object.isArray()
Return `true` if the object is an array, `false` otherwise.
Examples:
```
[].isArray();
\=> true
{}.isArray();
\=> false
```
## Array Prototypes

@@ -281,1 +329,8 @@

### License
This package is published under the MIT license.
You can integrate it in any commercial, closed software and/or make changes to the code with complete liberty.
If you send your changes back to the main repo we will be grateful,
but it is by no means required.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc