prototypes
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -8,3 +8,2 @@ 'use strict'; | ||
// requires | ||
@@ -19,3 +18,1 @@ require('./lib/string.js'); | ||
exports.overwriteWith(mathLib); | ||
'use strict'; | ||
/* globals escape, unescape */ | ||
@@ -206,2 +207,6 @@ /** | ||
character = character || ' '; | ||
if (length - this.length <= 0) | ||
{ | ||
return this; | ||
} | ||
return character.repeat(length - this.length) + this; | ||
@@ -220,4 +225,41 @@ }; | ||
/** | ||
* Web safe escape. Escapes everything that escape does and the plus sign. | ||
*/ | ||
newString.escapeForWeb = function() | ||
{ | ||
return escape(this).replaceAll('+', '%20'); | ||
}; | ||
/** | ||
* Unescapes everything that unescape does plus "+", and can also be applied | ||
* on the result more than once without generating URIError: URI malformed as decodeURIComponent does. | ||
*/ | ||
newString.unescapeForWeb = function() | ||
{ | ||
return unescape(this.replaceAll('%20', ' ')); | ||
}; | ||
/** | ||
* Implement a hash code prototype for a string. | ||
* Based on http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/ | ||
*/ | ||
newString.hashCode = function() | ||
{ | ||
var hash = 0; | ||
if (this.length === 0) | ||
{ | ||
return hash; | ||
} | ||
for (var i = 0; i < this.length; i++) | ||
{ | ||
var char = this.charCodeAt(i); | ||
hash = ((hash<<5)-hash)+char; | ||
hash = hash & hash; // Convert to 32bit integer | ||
} | ||
return hash; | ||
}; | ||
// add new string functions to string prototype | ||
core.addProperties(String.prototype, newString); | ||
{ | ||
"name": "prototypes", | ||
"version": "1.2.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().", | ||
"homepage": "https://github.com/alexfernandez/prototypes", | ||
"contributors": ["Alex Fernández <alexfernandeznpm@gmail.com>"], | ||
"license": "MIT", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/alexfernandez/prototypes" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"testing": "*" | ||
}, | ||
"keywords" : ["prototypes", "prototype", "string", "object", "functional programming", "functional"], | ||
"engines": { | ||
"node": "*" | ||
}, | ||
"preferGlobal": false, | ||
"scripts": { | ||
"test": "node runTests.js" | ||
}, | ||
"private": false | ||
"name": "prototypes", | ||
"version": "1.3.0", | ||
"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", | ||
"contributors": [ | ||
"Alex Fernández <alexfernandeznpm@gmail.com>" | ||
], | ||
"license": "MIT", | ||
"main": "index.js", | ||
"files": [ | ||
"index.js", | ||
"lib" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/alexfernandez/prototypes" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"testing": "*" | ||
}, | ||
"keywords": [ | ||
"prototypes", | ||
"prototype", | ||
"string", | ||
"object", | ||
"functional programming", | ||
"functional" | ||
], | ||
"engines": { | ||
"node": "*" | ||
}, | ||
"preferGlobal": false, | ||
"scripts": { | ||
"test": "node runTests.js" | ||
}, | ||
"private": false | ||
} |
287
README.md
@@ -27,3 +27,5 @@ [![Build Status](https://secure.travis-ci.org/alexfernandez/prototypes.png)](http://travis-ci.org/alexfernandez/prototypes) | ||
require('prototypes'); | ||
```js | ||
require('prototypes'); | ||
``` | ||
@@ -46,4 +48,6 @@ There is no need to assign the result to any variable, since the prototypes | ||
'pepitus'.startsWith('pep'); | ||
\=> true | ||
```js | ||
'pepitus'.startsWith('pep'); | ||
//=> true | ||
``` | ||
@@ -54,4 +58,6 @@ ### string.endsWith(str) | ||
'pepitus'.endsWith('tus'); | ||
\=> true | ||
```js | ||
'pepitus'.endsWith('tus'); | ||
//=> true | ||
``` | ||
@@ -64,4 +70,6 @@ ### string.substringUpTo(str) | ||
'hi.there'.substringUpTo('.'); | ||
\=> 'hi' | ||
```js | ||
'hi.there'.substringUpTo('.'); | ||
//=> 'hi' | ||
``` | ||
@@ -74,4 +82,6 @@ ### string.substringUpToLast(str) | ||
'hi.there.you'.substringUpToLast('.'); | ||
\=> 'hi.there' | ||
```js | ||
'hi.there.you'.substringUpToLast('.'); | ||
//=> 'hi.there' | ||
``` | ||
@@ -83,4 +93,6 @@ ### string.substringFrom(str) | ||
'hi.there'.substringFrom('.'); | ||
\=> 'there' | ||
```js | ||
'hi.there'.substringFrom('.'); | ||
//=> 'there' | ||
``` | ||
@@ -92,12 +104,16 @@ ### string.substringFromLast(str) | ||
'hi.there.you'.substringFromLast('.'); | ||
\=> 'you' | ||
```js | ||
'hi.there.you'.substringFromLast('.'); | ||
//=> 'you' | ||
``` | ||
### string.contains(str) | ||
Find out if the string contains the argument at any position. | ||
Find out if the string contains the argument at any position. Case-sensitive. | ||
Example: | ||
'abcde'.contains('bcd'); | ||
\=> true | ||
```js | ||
'abcde'.contains('bcd'); | ||
//=> true | ||
``` | ||
@@ -107,7 +123,9 @@ ### string.containsIgnoreCase(str) | ||
Find out if the string contains the argument at any position, | ||
ignoring case. | ||
case-insensitive. | ||
Example: | ||
'aBcDe'.contains('bCd'); | ||
\=> true | ||
```js | ||
'aBcDe'.containsIgnoreCase('bCd'); | ||
//=> true | ||
``` | ||
@@ -119,4 +137,6 @@ ### string.replaceAll(str, replacement) | ||
'pepitus'.replaceAll('p', 'c'); | ||
\=> 'cecitus' | ||
```js | ||
'pepitus'.replaceAll('p', 'c'); | ||
//=> 'cecitus' | ||
``` | ||
@@ -128,4 +148,6 @@ ### string.replaceIgnoreCase(str, replacement) | ||
'Pepitus'.replaceAll('p', 'c'); | ||
\=> 'cecitus' | ||
```js | ||
'Pepitus'.replaceAll('p', 'c'); | ||
//=> 'cecitus' | ||
``` | ||
@@ -138,4 +160,6 @@ ### string.replaceAllIgnoreCase(str, replacement) | ||
'Pepitus'.replaceAll('p', 'cor'); | ||
\=> 'corecoritus' | ||
```js | ||
'Pepitus'.replaceAll('p', 'cor'); | ||
//=> 'corecoritus' | ||
``` | ||
@@ -147,4 +171,6 @@ ### string.repeat(number) | ||
'ab'.repeat(3); | ||
\=> 'ababab' | ||
```js | ||
'ab'.repeat(3); | ||
//=> 'ababab' | ||
``` | ||
@@ -156,4 +182,6 @@ ### string.capitalize() | ||
'hello'.capitalize(); | ||
\=> 'Hello' | ||
```js | ||
'hello'.capitalize(); | ||
//=> 'Hello' | ||
``` | ||
@@ -166,7 +194,40 @@ ### string.format() | ||
``` | ||
```js | ||
'Hi %s, %j'.format('a', {}); | ||
\=> 'Hi a, {}' | ||
//=> 'Hi a, {}' | ||
``` | ||
### string.escapeForWeb() | ||
Web safe escape. Escapes everything that escape does and the plus sign. | ||
Example: | ||
``` | ||
'Hi, my name is Pepíto'.escapeForWeb(); | ||
\=> 'Hi%2C%20my%20name%20is%20Pep%EDto' | ||
``` | ||
### string.unescapeForWeb() | ||
Unescapes everything that unescape does plus "+", | ||
and can also be applied on the result more than once without generating `URIError: | ||
URI malformed` as `decodeURIComponent()` does. | ||
Example: | ||
``` | ||
'Hi%2C%20my%20name%20is%20Pep%EDto'.unescapeForWeb(); | ||
\=> 'Hi, my name is Pepíto' | ||
``` | ||
### string.hashCode() | ||
Implement a hash code prototype for a string. | ||
Based on [Manwe's function](http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/). | ||
Example: | ||
``` | ||
'Hi, my name is Pepíto'.hashCode(); | ||
\=> 1239770349 | ||
``` | ||
## Object Prototypes | ||
@@ -187,12 +248,16 @@ | ||
{a: 'a'}.countProperties(); | ||
\=> 1 | ||
```js | ||
{a: 'a'}.countProperties(); | ||
//=> 1 | ||
``` | ||
You can also pass a string or a function as a filter: | ||
{hello: 'a'}.countProperties('ll'); | ||
\=> 1 | ||
```js | ||
{hello: 'a'}.countProperties('ll'); | ||
//=> 1 | ||
{hello: 'a'}.countProperties(function(key) { return key.length == 5 }); | ||
\=> 1 | ||
{hello: 'a'}.countProperties(function(key) { return key.length == 5 }); | ||
//=> 1 | ||
``` | ||
@@ -204,4 +269,6 @@ ### object.overwriteWith(overwriter) | ||
{a: 'a'}.overwriteWith({b: 'b'}); | ||
\=> {a: 'a', b: 'b'} | ||
```js | ||
{a: 'a'}.overwriteWith({b: 'b'}); | ||
//=> {a: 'a', b: 'b'} | ||
``` | ||
@@ -211,4 +278,6 @@ *Note*: properties which are `undefined` are not overwritten; | ||
{a: 'a'}.overwriteWith({b: undefined, c: null}); | ||
\=> {a: 'a', c: null} | ||
```js | ||
{a: 'a'}.overwriteWith({b: undefined, c: null}); | ||
//=> {a: 'a', c: null} | ||
``` | ||
@@ -221,4 +290,6 @@ ### object.concat(otherObject) | ||
{a: 'a'}.concat({b: 'b'}); | ||
\=> {a: 'a', b: 'b'} | ||
```js | ||
{a: 'a'}.concat({b: 'b'}); | ||
//=> {a: 'a', b: 'b'} | ||
``` | ||
@@ -232,9 +303,8 @@ ### object.forEach(callback) | ||
``` | ||
{a: 1, b: 2}.forEach(function(value, key) | ||
{ | ||
console.log(key + ': ' + value); | ||
```js | ||
{a: 1, b: 2}.forEach(function(value, key) { | ||
console.log(key + ': ' + value); | ||
}); | ||
\=> a: 1 | ||
b: 2 | ||
//=> a: 1 | ||
//=> b: 2 | ||
``` | ||
@@ -251,8 +321,7 @@ | ||
``` | ||
{a: 1, b: 2}.filterIn(function(value) | ||
{ | ||
return value > 1; | ||
```js | ||
{a: 1, b: 2}.filterIn(function(value) { | ||
return value > 1; | ||
}); | ||
\=> {b: 2} | ||
//=> {b: 2} | ||
``` | ||
@@ -269,8 +338,7 @@ | ||
``` | ||
{a: 1, b: 2}.filterOut(function(value) | ||
{ | ||
```js | ||
{a: 1, b: 2}.filterOut(function(value) { | ||
return value > 1; | ||
}); | ||
\=> {a: 1} | ||
//=> {a: 1} | ||
``` | ||
@@ -286,4 +354,6 @@ | ||
Object.values({first: 'a', second: 'b'}); | ||
\=> ['a', 'b'] | ||
```js | ||
Object.values({first: 'a', second: 'b'}); | ||
//=> ['a', 'b'] | ||
``` | ||
@@ -298,4 +368,6 @@ ## Array Prototypes | ||
['a', 'b'].contains('a'); | ||
\=> true | ||
```js | ||
['a', 'b'].contains('a'); | ||
//=> true | ||
``` | ||
@@ -307,8 +379,9 @@ ### array.remove(element) | ||
``` | ||
```js | ||
var array = ['a', 'b']; | ||
array.remove('a'); | ||
\=> 'a' | ||
//=> 'a' | ||
array | ||
\=> ['b'] | ||
//=> ['b'] | ||
``` | ||
@@ -326,8 +399,7 @@ | ||
``` | ||
['a', 'b', 'c1', 'c2'].filterOut(function(element) | ||
{ | ||
return element.startsWith('c'); | ||
```js | ||
['a', 'b', 'c1', 'c2'].filterOut(function(element) { | ||
return element.startsWith('c'); | ||
}); | ||
\=> ['a', 'b'] | ||
//=> ['a', 'b'] | ||
``` | ||
@@ -340,5 +412,5 @@ | ||
``` | ||
```js | ||
['c', 'a', 'b', 'c', 'b'].unique(); | ||
\=> ['c', 'a', 'b'] | ||
//=> ['c', 'a', 'b'] | ||
``` | ||
@@ -357,5 +429,5 @@ | ||
``` | ||
```js | ||
['a', 'b', 'c'].first(); | ||
\=> 'a' | ||
//=> 'a' | ||
``` | ||
@@ -368,5 +440,5 @@ | ||
``` | ||
```js | ||
['a', 'b', 'c'].last(); | ||
\=> 'c' | ||
//=> 'c' | ||
``` | ||
@@ -378,5 +450,5 @@ | ||
``` | ||
```js | ||
[1, 2, [3, 4, [5, 6]]].concatAll(); | ||
\=> [1, 2, 3, 4, [5, 6]] | ||
//=> [1, 2, 3, 4, [5, 6]] | ||
``` | ||
@@ -388,5 +460,5 @@ | ||
``` | ||
```js | ||
[1, 2, [3, 4, [5, 6]]].concatAll(); | ||
\=> [1, 2, 3, 4, 5, 6] | ||
//=> [1, 2, 3, 4, 5, 6] | ||
``` | ||
@@ -400,5 +472,5 @@ | ||
``` | ||
```js | ||
Array.toArray({a: 1, b: 2}); | ||
\=> [1, 2] | ||
//=> [1, 2] | ||
``` | ||
@@ -423,5 +495,7 @@ | ||
// unsafe parseInt() | ||
parseInt('010'); | ||
\=> 8 | ||
```js | ||
// unsafe parseInt() | ||
parseInt('010'); | ||
//=> 8 | ||
``` | ||
@@ -434,4 +508,6 @@ This library replaces the global function with a safe version that uses radix 10 | ||
parseInt('010'); | ||
\=> 10 | ||
```js | ||
parseInt('010'); | ||
//=> 10 | ||
``` | ||
@@ -445,8 +521,12 @@ ### isNumber(value) | ||
var prototypes = require('prototypes'); | ||
prototypes.isNumber(5); | ||
\=> true | ||
prototypes.isNumber('hi'); | ||
\=> false | ||
```js | ||
var prototypes = require('prototypes'); | ||
prototypes.isNumber(5); | ||
//=> true | ||
prototypes.isNumber('hi'); | ||
//=> false | ||
``` | ||
### Math.log10(number) | ||
@@ -456,4 +536,6 @@ | ||
Math.log10(10); | ||
\=> 1 | ||
```js | ||
Math.log10(10); | ||
//=> 1 | ||
``` | ||
@@ -464,10 +546,13 @@ ### number.toRad() | ||
var n = 180; | ||
n.toRad(); | ||
\=> 3.141592653589793 | ||
```js | ||
var n = 180; | ||
n.toRad(); | ||
//=> 3.141592653589793 | ||
``` | ||
## RegExp Prototypes | ||
Prototypes used to enhance regular expressions (the RegExp prototype). Can also be used with the syntax | ||
/.../. | ||
`/.../.` | ||
@@ -479,4 +564,6 @@ ### makeGlobal() | ||
'pepitus'.replace(/p/.makeGlobal(), 'c'); | ||
\=> 'cecitus' | ||
```js | ||
'pepitus'.replace(/p/.makeGlobal(), 'c'); | ||
//=> 'cecitus' | ||
``` | ||
@@ -483,0 +570,0 @@ ## Acknowledgements |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
547
0
26938
9
629