Permalink
Browse files

Initial commit

  • Loading branch information...
Eric Hamilton
Eric Hamilton committed Sep 1, 2012
0 parents commit f507d971a70da224d3eb447ed87ddbeb1b9fd097
Showing with 2,200 additions and 0 deletions.
  1. +9 −0 LICENSE
  2. +23 −0 README.markdown
  3. +71 −0 dist/cuid.js
  4. +42 −0 grunt.js
  5. +34 −0 package.json
  6. +71 −0 src/cuid.js
  7. +20 −0 test/index.html
  8. +226 −0 test/qunit.css
  9. +1,597 −0 test/qunit.js
  10. +25 −0 test/test.js
  11. +72 −0 test/travis/run-qunit.js
  12. +10 −0 test/travis/server.js
@@ -0,0 +1,9 @@
+cuid is covered by The MIT License:
+
+Copyright (c) 2012 Eric Elliott
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,23 @@
+# CUID
+
+Collision-resistant client-side UID generator safe for element IDs and server-side lookups.
+
+.cuid() returns a short random string prepended with the current time and a little browser/page fingerprint to prevent collisions from different clients.
+
+## Example
+
+wz5347t5lh1ttmg
+
+### Broken down
+
+** w - z5347t - [5l-h1] - ttmg **
+
+The first char is a random a-z char to make it HTML ID friendly.
+
+The next group is a timestamp.
+
+After that is a fingerprint where the first chars are obtained from the user agent string (which is fairly unique), and the supported mimeTypes (which is also fairly unique, except for IE, which always returns 0).
+
+The final bit of browser fingerprint is a count of global variables (which varies a lot).
+
+The rest of the ID is just a random number.
@@ -0,0 +1,71 @@
+/**
+ * cuid.js
+ * Collision-resistant client-side UID generator safe
+ * for element IDs and server-side lookups.
+ *
+ * Extracted from CLCTR
+ *
+ * Copyright (c) Eric Elliott 2012
+ * MIT License
+ */
+
+/*global window, navigator */
+(function () {
+ 'use strict';
+ var globalCount = (function () {
+ var i,
+ count = 0;
+ for (i in window) {
+ count++;
+ }
+ return count;
+ }()),
+
+ /**
+ * cuid returns a short random string prepended with
+ * the current time and a little browser/page fingerprint
+ * to prevent collisions from different clients.
+ *
+ * Sample: wz5347t5lh1ttmg
+ * Broken down: w - z5347t - 5lh1 - ttmg
+ * The first char is a random a-z char to make it HTML
+ * ID friendly.
+ *
+ * The next group is a timestamp.
+ *
+ * After that is a fingerprint where the first chars
+ * are obtained from the user agent string (which
+ * is fairly unique), and the supported mimeTypes
+ * (which is also fairly unique, except for IE, which
+ * always returns 0).
+ *
+ * The final bit of browser fingerprint is a count of
+ * global variables (which varies a lot).
+ *
+ * The rest of the ID is just a random number.
+ **/
+ cuid = function cuid() {
+ // Starting with a lowercase letter Makes
+ // it HTML element ID friendly.
+ var letter = 'abcdefghijklmnopqrstuvwxyz'
+ .charAt(Math.floor(Math.random()*26)),
+
+ // << 0 shaves least useful digits from timestamp
+ timestamp = (new Date().getTime() << 0).toString(36),
+
+ // A few chars to generate distinct ids for different
+ // browser / page combos (so different computers are far less
+ // likely to generate the same id)
+ fingerprint = (navigator.mimeTypes.length +
+ navigator.userAgent.length).toString(36) +
+ globalCount.toString(36),
+
+ // Grab some more chars from browser's .random() method
+ random = ("0000" + (Math.random() *
+ Math.pow(36, 4) << 0)
+ .toString(36)).substr(-4);
+ return (letter + timestamp + fingerprint + random);
+ };
+
+ window.cuid = cuid;
+}());
@@ -0,0 +1,42 @@
+/*global module*/
+module.exports = function(grunt) {
+ 'use strict';
+ grunt.initConfig({
+ pkg: '<json:package.json>',
+ lint: {
+ all: ['./grunt.js', './src/**/*.js', './test/test.js']
+ },
+ jshint: {
+ options: {
+ curly: true,
+ eqeqeq: true,
+ immed: true,
+ latedef: true,
+ newcap: true,
+ nonew: true,
+ noarg: true,
+ sub: true,
+ undef: true,
+ unused: true,
+ eqnull: true,
+ browser: true,
+ strict: true,
+ boss: false
+ }
+ },
+ concat: {
+ dist: {
+ src: ['src/cuid.js'],
+ dest: 'dist/cuid.js'
+ }
+ },
+ qunit: {
+ index: ['test/index.html']
+ },
+ watch: {
+ files: ['<config:lint.all>'],
+ tasks: ['lint', 'concat,', 'qunit']
+ }
+ });
+ grunt.registerTask('default', 'lint concat qunit');
+};
@@ -0,0 +1,34 @@
+{
+ "name": "cuid",
+ "version": "0.1.0",
+ "description": "Client-side unique ids",
+ "author": {
+ "name": "Eric Elliott",
+ "url": "http://ericleads.com"
+ },
+ "main": "./src/cuid.js",
+ "keywords": [
+ "uid",
+ "unique id",
+ "guid",
+ "uuid"
+ ],
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/dilvie/cuid.git"
+ },
+ "directories": {
+ "test": "./test",
+ "src": "./src",
+ "dist": "./dist"
+ },
+ "dependencies": {
+ "grunt": "~0.3.x"
+ },
+ "devDependencies": {
+ "tap": "~0.2.4"
+ },
+ "engines": {
+ "node": "~0.8.x"
+ }
+}
@@ -0,0 +1,71 @@
+/**
+ * cuid.js
+ * Collision-resistant client-side UID generator safe
+ * for element IDs and server-side lookups.
+ *
+ * Extracted from CLCTR
+ *
+ * Copyright (c) Eric Elliott 2012
+ * MIT License
+ */
+
+/*global window, navigator */
+(function () {
+ 'use strict';
+ var globalCount = (function () {
+ var i,
+ count = 0;
+ for (i in window) {
+ count++;
+ }
+ return count;
+ }()),
+
+ /**
+ * cuid returns a short random string prepended with
+ * the current time and a little browser/page fingerprint
+ * to prevent collisions from different clients.
+ *
+ * Sample: wz5347t5lh1ttmg
+ * Broken down: w - z5347t - 5lh1 - ttmg
+ * The first char is a random a-z char to make it HTML
+ * ID friendly.
+ *
+ * The next group is a timestamp.
+ *
+ * After that is a fingerprint where the first chars
+ * are obtained from the user agent string (which
+ * is fairly unique), and the supported mimeTypes
+ * (which is also fairly unique, except for IE, which
+ * always returns 0).
+ *
+ * The final bit of browser fingerprint is a count of
+ * global variables (which varies a lot).
+ *
+ * The rest of the ID is just a random number.
+ **/
+ cuid = function cuid() {
+ // Starting with a lowercase letter Makes
+ // it HTML element ID friendly.
+ var letter = 'abcdefghijklmnopqrstuvwxyz'
+ .charAt(Math.floor(Math.random()*26)),
+
+ // << 0 shaves least useful digits from timestamp
+ timestamp = (new Date().getTime() << 0).toString(36),
+
+ // A few chars to generate distinct ids for different
+ // browser / page combos (so different computers are far less
+ // likely to generate the same id)
+ fingerprint = (navigator.mimeTypes.length +
+ navigator.userAgent.length).toString(36) +
+ globalCount.toString(36),
+
+ // Grab some more chars from browser's .random() method
+ random = ("0000" + (Math.random() *
+ Math.pow(36, 4) << 0)
+ .toString(36)).substr(-4);
+ return (letter + timestamp + fingerprint + random);
+ };
+
+ window.cuid = cuid;
+}());
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+ <html>
+ <head>
+ </head>
+ <body>
+ <section>
+ <h1 id="qunit-header">QUnit Test Suite</h1>
+ <h2 id="qunit-banner"></h2>
+ <div id="qunit-testrunner-toolbar"></div>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+ </section>
+
+ <script src="../dist/cuid.js"></script>
+
+ <script src="qunit.js"></script>
+ <script src="test.js"></script>
+ <link href="qunit.css" rel="stylesheet"></style>
+ </body>
+ </html>
Oops, something went wrong.

0 comments on commit f507d97

Please sign in to comment.