
'use strict'

var namespace = 'cuid'
var c = 0
var blocksize = 4
var base = 36
var discreteValues = Math.pow(base, blocksize)

var pad = function pad (num, size) {
  var s = '000000000' + num
  return s.substr(s.length - size)
}

var randomBlock = function randomBlock () {
  return pad((Math.random() * discreteValues << 0)
    .toString(base), blocksize)
}

var safeCounter = function () {
  c = (c < discreteValues) ? c : 0
  c++
  return c - 1
}

function cuid () {
  // Starting with a lowercase letter makes
  // it HTML element ID friendly.
  var letter = 'c' // hard-coded allows for sequential access
  var timestamp = (new Date().getTime()).toString(base)
  var counter
  var finger = nodePrint()
  var random = randomBlock() + randomBlock()

  counter = pad(safeCounter().toString(base), blocksize)

  return (letter + timestamp + counter + finger + random)
}

function slug () {
  var counter
  var date = new Date().getTime().toString(36)
  var print = nodePrint().slice(0, 1) + nodePrint.slice(-1)
  var random = randomBlock().slice(-2)

  counter = safeCounter().toString(36).slice(-4)

  return date.slice(-2) + counter + print + random
}

function nodePrint () {
  var os = require('os')
  var padding = 2
  var pid = pad((process.pid).toString(36), padding)
  var hostname = os.hostname()
  var length = hostname.length
  var hostId = pad((hostname)
    .split('')
    .reduce(function (prev, char) {
      return +prev + char.charCodeAt(0)
    }, +length + 36)
    .toString(36), padding
  )

  return pid + hostId
}

function globalCount() {
  // We want to cache the results of this
  var cache = (function calc() {
    var i
    var count = 0

    for (i in window) {
      count++
    }

    return count
  }());

  return cache;
}

function browserPrint() {
  return pad((navigator.mimeTypes.length
    + navigator.userAgent.length).toString(36)
    + globalCount().toString(36), 4);
};

module.exports = cuid
module.exports.slug = slug


@@ -1527,24 +1527,78 @@
 + hostId%0A%7D%0A%0A
+// NOTE: browser code not in use in this library.%0A%0A// 
 function glo
@@ -1606,24 +1606,27 @@
 alCount() %7B%0A
+// 
   // We want
@@ -1655,16 +1655,19 @@
 of this%0A
+// 
   var ca
@@ -1687,24 +1687,27 @@
 on calc() %7B%0A
+// 
     var i%0A  
@@ -1704,16 +1704,19 @@
   var i%0A
+// 
     var 
@@ -1726,20 +1726,23 @@
 nt = 0%0A%0A
+//
     
+ 
 for (i i
@@ -1757,22 +1757,25 @@
 ) %7B%0A
+//
       
+ 
 count++%0A
     
@@ -1774,19 +1774,25 @@
 t++%0A
+//
     
+ 
 %7D%0A%0A
+//
     
+ 
 retu
@@ -1800,16 +1800,19 @@
 n count%0A
+// 
   %7D());%0A
@@ -1812,16 +1812,19 @@
  %7D());%0A%0A
+// 
   return
@@ -1831,19 +1831,25 @@
  cache;%0A
-%7D%0A%0A
+// %7D%0A%0A// 
 function
@@ -1862,24 +1862,27 @@
 erPrint() %7B%0A
+// 
   return pad
@@ -1910,16 +1910,19 @@
 .length%0A
+// 
     + na
@@ -1956,24 +1956,27 @@
 oString(36)%0A
+// 
     + global
@@ -2001,16 +2001,19 @@
 6), 4);%0A
+// 
 %7D;%0A%0Amodu

update random/cuid — comment out browser code keeping focus server side.