define-uniqueid
Advanced tools
Comparing version 0.0.0 to 0.0.3
{ | ||
"name": "define-uniqueid", | ||
"version": "0.0.0", | ||
"version": "0.0.3", | ||
"homepage": "https://github.com/dicksont/define-uniqueid", | ||
@@ -5,0 +5,0 @@ "authors": [ |
{ | ||
"name": "define-uniqueid", | ||
"version": "0.0.0", | ||
"version": "0.0.3", | ||
"description": "uniqueId property generator for arbitrary object types", | ||
@@ -8,3 +8,5 @@ "main": "uniqid.js", | ||
"devDependencies": { | ||
"requirejs": "~2.1.18" | ||
"requirejs": "~2.1.18", | ||
"mocha": "~2.2.4", | ||
"jsdom": "~5.4.3" | ||
}, | ||
@@ -11,0 +13,0 @@ "scripts": { |
###define-uniqueid | ||
[![Build Status](https://travis-ci.org/dicksont/define-uniqueid.svg?branch=master)](https://travis-ci.org/dicksont/define-uniqueid) [![npm version](https://badge.fury.io/js/define-uniqueid.svg)](http://badge.fury.io/js/define-uniqueid) [![Bower version](https://badge.fury.io/bo/define-uniqueid.svg)](http://badge.fury.io/bo/define-uniqueid) | ||
Define-uniqueid is a sequential, unique id generator that can be attached to arbitrary object prototypes. | ||
[License](LICENSE) | ||
## Usage | ||
### DOM / Chrome / Safari / Firefox / IE / Opera | ||
```html | ||
<script src="/bower_components/define-uniqueid/uniqid.js"></script> | ||
<script> | ||
var undefineFx = defineUniqueId(HTMLElement); | ||
var div = document.createElement('div'); | ||
div.uniqueId; | ||
</script> | ||
``` | ||
### CommonJS / Node | ||
@@ -12,4 +26,3 @@ ```javascript | ||
var defineUniqueId = require('define-uniqueid').defineUniqueId; | ||
defineUniqueId(Object); | ||
var undefineFx = defineUniqueId(Object); | ||
({}).uniqueId; | ||
@@ -30,3 +43,3 @@ | ||
requirejs(['define-uniqueid'], function(defineUniqueId) { | ||
defineUniqueId(Object); | ||
var undefineFx = defineUniqueId(Object); | ||
({}).uniqueId; | ||
@@ -36,12 +49,1 @@ }); | ||
``` | ||
### Browser | ||
```html | ||
<script src="/bower_components/define-uniqueid/uniqid.js"></script> | ||
<script> | ||
defineUniqueId(HTMLElement); | ||
var div = document.createElement('div'); | ||
div.uniqueId; | ||
</script> | ||
``` |
103
spec/spec.js
@@ -27,22 +27,21 @@ /* | ||
'use strict' | ||
var assert = require('assert'); | ||
var requirejs = require('requirejs'); | ||
var defineUniqueId; | ||
var path = require('path'); | ||
function test() { | ||
var a = {}; | ||
var b = {}; | ||
var mainPath = path.resolve(__dirname,'../uniqid.js') | ||
after(function(done) { | ||
delete a.uniqueId; | ||
delete b.uniqueId; | ||
delete Object.prototype.uniqueId; | ||
done(); | ||
}); | ||
var TEST; | ||
it('should add a uniqueId property to Object', function() { | ||
function addTests() { | ||
var undefine; | ||
it('should add a uniqueId property to target prototype', function() { | ||
var a = TEST.targetGenerator(); | ||
assert.equal(a.uniqueId, null); | ||
defineUniqueId(Object, { static: true, redefine: true }); | ||
undefine = TEST.defineUniqueId(); | ||
assert.notEqual(a.uniqueId, null); | ||
@@ -56,2 +55,3 @@ assert.equal(a.uniqueId, a.uniqueId); | ||
it('should be consistent', function() { | ||
var a = TEST.targetGenerator(); | ||
var id = a.uniqueId; | ||
@@ -63,2 +63,5 @@ assert.notEqual(id, null); | ||
it('should be unique', function() { | ||
var a = TEST.targetGenerator(); | ||
var b = TEST.targetGenerator(); | ||
assert.notEqual(a.uniqueId, null); | ||
@@ -68,7 +71,37 @@ assert.notEqual(a.uniqueId, b.uniqueId); | ||
}); | ||
after(function(done) { | ||
undefine(); | ||
done(); | ||
}); | ||
} | ||
describe('defineUniqueId [CommonJS + format ]', function(){ | ||
var defineUniqueId = require(mainPath); | ||
TEST = { | ||
defineUniqueId: function() { | ||
defineUniqueId(Object, { | ||
static: true, | ||
redefine: true, | ||
format: function(id) { | ||
return 'prefix-' + id + '-suffix'; | ||
} | ||
}) | ||
}, | ||
targetGenerator: function() { return {} } | ||
}; | ||
addTests(); | ||
}); | ||
describe('defineUniqueId [CommonJS]', function(){ | ||
defineUniqueId = require('../uniqid.js'); | ||
test(); | ||
var defineUniqueId = require(mainPath); | ||
TEST = { | ||
defineUniqueId: function() { return defineUniqueId(Object, { static: true, redefine: true }) }, | ||
targetGenerator: function() { return {} } | ||
}; | ||
addTests(); | ||
}); | ||
@@ -85,4 +118,8 @@ | ||
requirejs(['define-uniqueid'], function(uniqueFx) { | ||
defineUniqueId = uniqueFx; | ||
requirejs(['define-uniqueid'], function(defineUniqueId) { | ||
TEST = { | ||
defineUniqueId: function() { return defineUniqueId(Object, { static: true, redefine: true }) }, | ||
targetGenerator: function() { return {} } | ||
}; | ||
done(); | ||
@@ -92,3 +129,33 @@ }); | ||
test(); | ||
addTests(); | ||
}); | ||
describe('defineUniqueId [DOM]', function() { | ||
before(function(done) { | ||
var jsdom = require('jsdom'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var uniqid = fs.readFileSync(mainPath, 'utf8'); | ||
jsdom.env({ | ||
html: '', | ||
src: [uniqid], | ||
loaded: function(errors, window) { | ||
if (errors != null) | ||
throw new Error('Failed to create jsDOM'); | ||
TEST = { | ||
defineUniqueId: function() { return window.defineUniqueId(window.HTMLElement, { static: true, redefine: true })}, | ||
targetGenerator: function() { return window.document.createElement('div') } | ||
}; | ||
done(); | ||
} | ||
}); | ||
}); | ||
addTests(); | ||
}); |
@@ -40,5 +40,6 @@ /* | ||
opts.enumerable = opts.enumerable || false; | ||
opts.prefix = opts.prefix || ''; | ||
opts.suffix = opts.suffix || ''; | ||
if (typeof(opts.format) != 'undefined' && typeof(opts.format) != 'function') { | ||
throw new Error('"format" property in option parameter is not a function'); | ||
} | ||
@@ -52,3 +53,3 @@ if (ctor.prototype.hasOwnProperty('uniqueId') && !opts.redefine) { | ||
Object.defineProperty(this, 'uniqueId', { | ||
value: opts.prefix + generateId() + opts.suffix, | ||
value: (opts.format)? opts.format(generateId()) : generateId(), | ||
writable: false, | ||
@@ -64,2 +65,9 @@ enumerable: opts.enumerable, | ||
}); | ||
function undefine() { | ||
delete ctor.prototype['uniqueId']; | ||
} | ||
return undefine; | ||
} | ||
@@ -66,0 +74,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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
10249
8
217
47
3
3