Created
April 11, 2013 07:48
-
-
Save ValeriiVasin/5361515 to your computer and use it in GitHub Desktop.
Minified classnames generator.
This file contains hidden or 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
/** | |
* Generate sequential minified classnames: | |
* a => b => .. => z => aa => ba => .. => zz => .. => aaa ... | |
*/ | |
var ClassNameEngine = (function () { | |
var alphabet = 'abcdefghijklmnopqrstuvwxyz', | |
alphabetLength = alphabet.length, | |
// current number combination which could be mapped to classname using alphabet | |
combination = null; | |
/** | |
* Last array index that is greater than number | |
* @param {Array} array Array to check | |
* @param {Number} number Number to compare with | |
* | |
* @example | |
* lastIndexLessThanNumber([0], 25) => 0 | |
* lastIndexLessThanNumber([26], 25) => -1 | |
* lastIndexLessThanNumber([0, 26]) => 0 | |
*/ | |
function lastIndexLessThanNumber(array, number) { | |
var found = false, | |
i = array.length - 1, | |
result = -1; | |
while (i >= 0 && result === -1) { | |
if (array[i] < number) { | |
result = i; | |
} | |
i -= 1; | |
} | |
return result; | |
} | |
/** | |
* Make all members greater than index zero | |
* @param {Array} array Array to zerofy | |
* @param {Number} index Index to start zerofy from | |
* | |
* @example | |
* zerofyFromIndex([1,2,3], 0) => [0, 0, 0] | |
* zerofyFromIndex([1,2,3], 1) => [1, 0, 0] | |
*/ | |
function zerofyFromIndex(array, index) { | |
var length = array.length; | |
// clone array, avoid side effect | |
array = [].concat(array); | |
while (index < length) { | |
array[index] = 0; | |
index += 1; | |
} | |
return array; | |
} | |
/** | |
* Returns classsName generated with alphabet and array of indexes | |
* @param {Array} array Array with indexes | |
* @return {String} Classname | |
*/ | |
function getClassName(array, alphabet) { | |
return array.map(function (value) { | |
return alphabet[value]; | |
}).join(''); | |
} | |
function nextCombination(combination) { | |
var lastIndex = combination.length - 1, | |
last = combination[lastIndex], | |
index; | |
// clone array, avoid side effect | |
combination = [].concat(combination); | |
// next classname with actual length | |
index = lastIndexLessThanNumber(combination, alphabetLength - 1); | |
if (index !== -1) { | |
combination[index] = combination[index] + 1; | |
return zerofyFromIndex(combination, index + 1); | |
} | |
// increase length of classname | |
if (last >= alphabetLength - 1) { | |
combination = zerofyFromIndex(combination, 0); | |
combination.push(0); | |
return combination; | |
} | |
} | |
return { | |
/** | |
* @return {String} next class name | |
*/ | |
next: function () { | |
combination = combination ? nextCombination(combination) : [0]; | |
return getClassName(combination, alphabet); | |
} | |
}; | |
}()); | |
// use it | |
for (var i = 0; i < 100; i += 1) { | |
console.log("%d. %s", i, ClassNameEngine.next() ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment