marchio-id-uuid
v0.1.1
Published
uuid promise-based generator and validator
Downloads
3
Readme
marchio-id-uuid
uuid promise-based generator and validator
Installation
$ npm init
$ npm install marchio-id-uuid --save
Usage
This package can generate v1 or v4 (default) uuids. It can also validate v1 through v5 uuids.
Generate default v4 uuid
var factory = require("marchio-id-uuid");
factory.create({})
.then(function(obj) {
return obj.generate();
})
.then(function(result) {
console.log("ID: ", result);
})
.catch( function(err) {
console.error(err);
});
Generate v1 uuid
var factory = require("marchio-id-uuid");
factory.create({})
.then(function(obj) {
return obj.generate( { version: "v1" } );
})
.then(function(result) {
console.log("ID: ", result);
})
.catch( function(err) {
console.error(err);
});
Browser Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>marchio-id-uuid example</title>
<meta name="description" content="marchio-id-uuid example">
<!-- either cdn should work once tagged and published -->
<!--
<script src="https://cdn.rawgit.com/mitchallen/marchio-id-uuid/v0.1.0/dist/marchio-id-uuid.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/marchio-id-uuid.min.js"></script>
-->
<script src="https://unpkg.com/[email protected]/dist/marchio-id-uuid.min.js"></script>
<script>
// Note that the 'window.'' prefix is not needed by most if not all browsers
// var factory = window.MitchAllen.MarchioIdUuid;
var factory = MitchAllen.MarchioIdUuid;
factory.create({})
.then(function(obj) {
return obj.generate();
})
.then(function(result) {
console.log("ID: ", result);
})
.catch( function(err) {
console.error(err);
});
</script>
</head>
<body>
<h1>marchio-id-uuid example</h1>
<p>See JavaScript developer console for output.</p>
</body>
</html>
References:
- UUID 4122 RFC
- Universally unique identifier (wikipedia)
- uuid (npmjs.com)
- https://gist.github.com/jed/982883
- https://gist.github.com/LeverOne/1308368
Modules
marchio-id-uuid
Module
- marchio-id-uuid
- .package()
- .health()
- .generate(spec) ⇒ Promise
- .validate(uuid, [version]) ⇒ Promise
marchio-id-uuid.package()
Returns the package name
Kind: instance method of marchio-id-uuid
marchio-id-uuid.health()
Health check
Kind: instance method of marchio-id-uuid
Example (Usage Example)
var factory = require("marchio-id-uuid");
factory.create({})
.then(function(obj) {
return obj.health();
})
.then(function(result) {
console.log("HEALTH: ", result);
})
.catch( function(err) {
console.error(err);
});
marchio-id-uuid.generate(spec) ⇒ Promise
Generate ID String
Kind: instance method of marchio-id-uuid
Returns: Promise - that resolves to a uuid based on the version
| Param | Type | Description | | --- | --- | --- | | spec | Object | Named parameters object | | [spec.version] | string | Optional version. Valid values: "v1" or "v4" (default) |
Example (Generate default v4 uuid)
var factory = require("marchio-id-uuid");
factory.create({})
.then(function(obj) {
return obj.generate();
})
.then(function(result) {
console.log("ID: ", result);
})
.catch( function(err) {
console.error(err);
});
Example (Generate v1 uuid)
var factory = require("marchio-id-uuid");
factory.create({})
.then(function(obj) {
return obj.generate( { version: "v1" } );
})
.then(function(result) {
console.log("ID: ", result);
})
.catch( function(err) {
console.error(err);
});
marchio-id-uuid.validate(uuid, [version]) ⇒ Promise
Validate ID String
Kind: instance method of marchio-id-uuid
Returns: Promise - that resolves to a uuid based on the version
| Param | Type | Description | | --- | --- | --- | | uuid | string | a uuid | | [version] | string | Optional version. Default to "v4". Valid values: "v1","v2","v3","v4", or "v5". |
Example (Validate version v4 (default) uuid)
var factory = require("marchio-id-uuid");
factory.create({})
.then(function(obj) {
return obj.validate('110ec58a-a0f2-4ac4-8393-c866d813b8d1');
})
.then(function(result) {
console.log( result ? "valid" : "invalid" );
})
.catch( function(err) {
console.error(err);
});
Example (Validate version v1 uuid)
var factory = require("marchio-id-uuid");
factory.create({})
.then(function(obj) {
return obj.validate(
'110ec58a-a0f2-1ac4-8393-c866d813b8d1',
'v1'
);
})
.then(function(result) {
console.log( result ? "valid" : "invalid" );
})
.catch( function(err) {
console.error(err);
});
marchio-id-uuid-factory
Factory module
marchio-id-uuid-factory.create(spec) ⇒ Promise
Factory method It takes one spec parameter that must be an object with named parameters
Kind: static method of marchio-id-uuid-factory
Returns: Promise - that resolves to {module:marchio-id-uuid}
| Param | Type | Description | | --- | --- | --- | | spec | Object | Named parameters object |
Example (Usage example)
var factory = require("marchio-id-uuid");
factory.create({})
.then(function(obj) {
return obj.health();
})
.catch( function(err) {
console.error(err);
});
marchio-id-uuid-ERROR
Error module
| Param | Type | Description |
| --- | --- | --- |
| GENERATE_V1_V4_ONLY
| string | marchio-id-uuid.generate only supports version values of v1 or v4 |
| INVALID_VERSION_PARAMETER
| string | marchio-id-uuid.validate - invalid version parameter |
Example (Usage example)
.catch( (err) => {
if( err.message == _factory.ERROR.MODEL_MUST_BE_DEFINED ) {
...
}
}
Testing
To test, go to the root folder and type (sans $):
$ npm test
Repo(s)
Donations
In lieu of donations you can support this project by buying one of my books: http://amazon.com/author/mitch.allen
Contributing
In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.
Version History
Version 0.1.1
- Updated browser example and documentation
Version 0.1.0
- initial release