aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMisko Hevery2011-04-12 13:40:23 -0700
committerMisko Hevery2011-06-08 15:01:32 -0700
commit2a12f7dcaa078e1d6c3b5092e62dd5f404b8c3e4 (patch)
treebc0b46a0f3949f444b55dae92ee44ed0f6d88aa4
parent20ce797906bf20cfb1682a6bd1bb23c02aedc369 (diff)
downloadangular.js-2a12f7dcaa078e1d6c3b5092e62dd5f404b8c3e4.tar.bz2
Added nextUid() function for unified way of generating IDs in angular
-rw-r--r--src/Angular.js31
-rw-r--r--test/AngularSpec.js16
2 files changed, 47 insertions, 0 deletions
diff --git a/src/Angular.js b/src/Angular.js
index 7249fb69..26026cf3 100644
--- a/src/Angular.js
+++ b/src/Angular.js
@@ -114,6 +114,7 @@ var $$element = '$element',
angularCallbacks = extensionMap(angular, 'callbacks'),
nodeName_,
rngScript = /^(|.*\/)angular(-.*?)?(\.min)?.js(\?[^#]*)?(#(.*))?$/,
+ uid = ['0', '0', '0'];
DATE_ISOSTRING_LN = 24;
/**
@@ -189,6 +190,36 @@ function formatError(arg) {
return arg;
}
+/**
+ * @description
+ * A consistent way of creating unique IDs in angular. The ID is a sequence of alpha numeric
+ * characters such as '012ABC'. The reason why we are not using simply a number counter is that
+ * the number string gets longer over time, and it can also overflow, where as the the nextId
+ * will grow much slower, it is a string, and it will never overflow.
+ *
+ * @returns an unique alpha-numeric string
+ */
+function nextUid() {
+ var index = uid.length;
+ var digit;
+
+ while(index) {
+ index--;
+ digit = uid[index].charCodeAt(0);
+ if (digit == 57 /*'9'*/) {
+ uid[index] = 'A';
+ return uid.join('');
+ }
+ if (digit == 90 /*'Z'*/) {
+ uid[index] = '0';
+ } else {
+ uid[index] = String.fromCharCode(digit + 1);
+ return uid.join('');
+ }
+ }
+ uid.unshift('0');
+ return uid.join('');
+}
/**
* @workInProgress
diff --git a/test/AngularSpec.js b/test/AngularSpec.js
index b58705cc..4ab14580 100644
--- a/test/AngularSpec.js
+++ b/test/AngularSpec.js
@@ -582,4 +582,20 @@ describe('angular', function(){
});
}
});
+
+
+ describe('nextUid()', function(){
+ it('should return new id per call', function(){
+ var seen = {};
+ var count = 100;
+
+ while(count--) {
+ var current = nextUid();
+ expect(current.match(/[\d\w]+/)).toBeTruthy();
+ expect(seen[current]).toBeFalsy();
+ seen[current] = true;
+ }
+ });
+ });
+
});