prototypes
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -11,2 +11,3 @@ 'use strict'; | ||
var util = require('util'); | ||
var crypto = require('crypto'); | ||
var core = require('./core.js'); | ||
@@ -18,2 +19,3 @@ | ||
/** | ||
@@ -272,1 +274,38 @@ * Make a new regular expression which is global. | ||
String.createToken = function(value) | ||
{ | ||
value = value || '' + Date.now() + Math.random(); | ||
var hash = crypto.createHash('sha256'); | ||
// instead of hex, return base36 which is more information-dense | ||
return convertBinaryToBase36(hash.update(String(value)).digest()); | ||
}; | ||
/** | ||
* Convert binary to base 36. | ||
*/ | ||
function convertBinaryToBase36(binary) | ||
{ | ||
var result = ''; | ||
for (var i = 0; i < 25; i++) | ||
{ | ||
var c; | ||
if (typeof binary == 'string') | ||
{ | ||
c = binary.charCodeAt(i) % 36; | ||
} | ||
else | ||
{ | ||
c = binary[i] % 36; | ||
} | ||
if (c < 10) | ||
{ | ||
result += String.fromCharCode(48 + c); | ||
} | ||
else | ||
{ | ||
result += String.fromCharCode(87 + c); | ||
} | ||
} | ||
return result; | ||
} | ||
{ | ||
"name": "prototypes", | ||
"version": "2.1.0", | ||
"version": "2.2.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().", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/alexfernandez/prototypes", |
@@ -230,2 +230,23 @@ [![Build Status](https://secure.travis-ci.org/alexfernandez/prototypes.png)](http://travis-ci.org/alexfernandez/prototypes) | ||
### String.createToken(value) | ||
Generate a new unique token in base36 | ||
(alphanumerical lowercase characters). | ||
It uses a random value and the current date, | ||
and optionally the given parameter. | ||
Should be quite strong, | ||
even if not cryptographically so. | ||
Example: | ||
```js | ||
String.createToken(); | ||
//=> 'dqna29cy639ekxtfiprdpg72h' | ||
``` | ||
*Note*: this function resides in the `Array` global like `Array.isArray()`, | ||
instead of in individual arrays as the previous functions. | ||
In versions up to 0.3.4 there were functions `object.toArray()` and `object.isArray()`, | ||
but they were removed due to | ||
[incompatibilities](https://github.com/alexfernandez/prototypes/issues/8) with lodash. | ||
## Object Prototypes | ||
@@ -232,0 +253,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
30363
720
605