-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rui Marques
committed
Nov 18, 2014
1 parent
412168c
commit c7dfd78
Showing
3 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
{ | ||
name: "angular-guid", | ||
version: "0.1.0", | ||
main: "./uuid.min.js", | ||
homepage: "https://github.com/ruionwriting/ngGuid", | ||
_source: "git@github.com:ruionwriting/ngGuid.git" | ||
main: "./guid.min.js", | ||
homepage: "https://github.com/ruionwriting/ngGuid" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/**! | ||
* ngGuid v0.1.0 | ||
* @copyright 2014 Rui Marques. All Rights Reserved. | ||
* @license see LICENCE. | ||
* [https://github.com:ruionwriting/ngUUID.git] | ||
* | ||
* Based on this SO discussion: http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript. | ||
*/ | ||
(function () { | ||
'use strict'; | ||
|
||
angular | ||
.module('ngGuid', []) | ||
.factory('Guid', Guid); | ||
|
||
function Guid() { | ||
var EMPTY = '00000000-0000-0000-0000-000000000000'; | ||
|
||
var service = { | ||
newGuid: create, | ||
empty: EMPTY | ||
}; | ||
|
||
return service; | ||
|
||
////////////// | ||
|
||
function _padLeft(paddingString, width, replacementChar) { | ||
return paddingString.length >= width ? paddingString : _padLeft(replacementChar + paddingString, width, replacementChar || ' '); | ||
} | ||
|
||
function _s4(number) { | ||
var hexadecimalResult = number.toString(16); | ||
|
||
return _padLeft(hexadecimalResult, 4, '0'); | ||
} | ||
|
||
function _cryptoGuid() { | ||
var buffer = new window.Uint16Array(8); | ||
window.crypto.getRandomValues(buffer); | ||
|
||
return [_s4(buffer[0]) + _s4(buffer[1]), _s4(buffer[2]), _s4(buffer[3]), _s4(buffer[4]), _s4(buffer[5]) + _s4(buffer[6]) + _s4(buffer[7])].join('-'); | ||
} | ||
|
||
function _guid() { | ||
var currentDateMilliseconds = new Date().getTime(); | ||
|
||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (currentChar) { | ||
var randomChar = (currentDateMilliseconds + Math.random() * 16) % 16 | 0; | ||
currentDateMilliseconds = Math.floor(currentDateMilliseconds / 16); | ||
|
||
return (currentChar === 'x' ? randomChar : (randomChar & 0x7 | 0x8)).toString(16); | ||
}); | ||
} | ||
|
||
function create() { | ||
var hasCrypto = typeof (window.crypto) != 'undefined', | ||
hasRandomValues = typeof (window.crypto.getRandomValues) != 'undefined'; | ||
|
||
return (hasCrypto && hasRandomValues) ? _cryptoGuid() : _guid(); | ||
} | ||
} | ||
|
||
}()); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.