forked from yui/yui3-gallery
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gallery-2012.12.05-21-01 solmsted gallery-any-base-converter
- Loading branch information
Showing
19 changed files
with
1,853 additions
and
431 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
build/gallery-any-base-converter/gallery-any-base-converter-coverage.js
Large diffs are not rendered by default.
Oops, something went wrong.
304 changes: 160 additions & 144 deletions
304
build/gallery-any-base-converter/gallery-any-base-converter-debug.js
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,168 +1,184 @@ | ||
YUI.add('gallery-any-base-converter', function(Y) { | ||
YUI.add('gallery-any-base-converter', function (Y, NAME) { | ||
|
||
/** | ||
* @module gallery-any-base-converter | ||
*/ | ||
(function (Y, moduleName) { | ||
'use strict'; | ||
|
||
var _string__empty = '', | ||
_string__fullStop = '.', | ||
_string_alphabet = 'alphabet', | ||
_string_lookup = 'lookup', | ||
_string_minusSign = 'minusSign', | ||
_string_radixPoint = 'radixPoint', | ||
|
||
_Base = Y.Base, | ||
|
||
_each = Y.each, | ||
_floor = Math.floor, | ||
_pow = Math.pow; | ||
|
||
/** | ||
* AnyBaseConverter is an object that will convert numbers to and from a | ||
* positional notation with a custom alphabet and base. | ||
* @class AnyBaseConverter | ||
* @extends Base | ||
* @param {Object} config Configuration Object. | ||
*/ | ||
Y.AnyBaseConverter = _Base.create(moduleName, _Base, [], { | ||
/** | ||
* Converts a string from a custom base and returns a number. | ||
* @method from | ||
* @param {String} any | ||
* @returns {Number} value | ||
*/ | ||
from: function (any) { | ||
any = any.split(this.get(_string_radixPoint)); | ||
|
||
var base = this.get(_string_alphabet).length, | ||
fractionalPart = any[1], | ||
integerPart = any[0].split(_string__empty), | ||
lookup = this.get(_string_lookup), | ||
negative = false, | ||
value = 0; | ||
|
||
if (integerPart[0] === this.get(_string_minusSign)) { | ||
negative = true; | ||
integerPart.shift(); | ||
} | ||
|
||
_each(integerPart.reverse(), function (character, index) { | ||
value += _pow(base, index) * lookup[character]; | ||
}); | ||
|
||
if (fractionalPart) { | ||
value = parseFloat(String(value) + _string__fullStop + String(this.from(fractionalPart)).split(_string__empty).reverse().join(_string__empty)); | ||
} | ||
|
||
if (negative) { | ||
value = -value; | ||
} | ||
|
||
return value; | ||
}, | ||
_pow = Math.pow, | ||
|
||
/** | ||
* Converts a number to a custom base and returns a string. | ||
* @method to | ||
* @param {Number} value | ||
* @returns {String} any | ||
*/ | ||
to: function (value) { | ||
value = +value; | ||
|
||
var alphabet = this.get(_string_alphabet), | ||
base = alphabet.length, | ||
fractionalPart, | ||
integerPart, | ||
any = _string__empty, | ||
negative = false; | ||
|
||
if (value < 0) { | ||
negative = true; | ||
value = -value; | ||
} | ||
|
||
integerPart = _floor(value); | ||
fractionalPart = String(value).split(_string__fullStop)[1]; | ||
|
||
do { | ||
any = alphabet.charAt(integerPart % base) + any; | ||
integerPart = _floor(integerPart / base); | ||
} while (integerPart); | ||
|
||
if (fractionalPart) { | ||
any += this.get(_string_radixPoint) + this.to(fractionalPart.split(_string__empty).reverse().join(_string__empty)); | ||
} | ||
|
||
if (negative) { | ||
any = this.get(_string_minusSign) + any; | ||
} | ||
|
||
return any; | ||
} | ||
}, { | ||
ATTRS: { | ||
/** | ||
* The string of characters to use as single-digit numbers. The | ||
* length of this string determines the base of the result. Each | ||
* character should be unique within the string or else it will be | ||
* impossible to correctly convert a string back into a number. | ||
* Currently, non-BMP characters are not supported. | ||
* @attribute alphabet | ||
* @default '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~' | ||
* @type String | ||
*/ | ||
alphabet: { | ||
setter: function (value) { | ||
var lookup = {}, | ||
i, | ||
length = value.length; | ||
|
||
for (i = 0; i < length; i += 1) { | ||
lookup[value.charAt(i)] = i; | ||
} | ||
|
||
this._set(_string_lookup, lookup); | ||
}, | ||
value: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~' | ||
}, | ||
* AnyBaseConverter is an object that will convert numbers to and from a | ||
* positional notation with a custom alphabet and base. | ||
* @class AnyBaseConverter | ||
* @constructor | ||
* @extends Base | ||
* @param {Object} config Configuration Object. | ||
*/ | ||
_class = _Base.create(moduleName, _Base, [], { | ||
/** | ||
* Used as a reverse lookup for a character index in alphabet. | ||
* @attribute lookup | ||
* @protected | ||
* @readOnly | ||
* @type Object | ||
*/ | ||
lookup: { | ||
readOnly: true, | ||
value: null | ||
* Converts a string from a custom base and returns a number. | ||
* @method from | ||
* @param {String} any | ||
* @return {Number} value | ||
*/ | ||
from: function (any) { | ||
var me = this, | ||
|
||
anySplit = any.split(me.get(_string_radixPoint)), | ||
base = me.get(_string_alphabet).length, | ||
fractionalPart = anySplit[1], | ||
integerPart = anySplit[0].split(_string__empty), | ||
lookup = me.get(_string_lookup), | ||
negative = false, | ||
value = 0; | ||
|
||
if (integerPart[0] === me.get(_string_minusSign)) { | ||
negative = true; | ||
integerPart.shift(); | ||
} | ||
|
||
_each(integerPart.reverse(), function (character, index) { | ||
value += _pow(base, index) * lookup[character]; | ||
}); | ||
|
||
if (fractionalPart) { | ||
value = parseFloat(_string__empty + value + _string__fullStop + _class._reverse(_string__empty + me.from(fractionalPart))); | ||
} | ||
|
||
if (negative) { | ||
value = -value; | ||
} | ||
|
||
return value; | ||
}, | ||
/** | ||
* A single character string to prepend to negative values. This | ||
* character should not be in the alphabet. | ||
* Currently, non-BMP characters are not supported. | ||
* @attribute minusSign | ||
* @default '-' | ||
* @type String | ||
*/ | ||
minusSign: { | ||
value: '-' | ||
* Converts a number to a custom base and returns a string. | ||
* @method to | ||
* @param {Number} value | ||
* @return {String} any | ||
*/ | ||
to: function (value) { | ||
var me = this, | ||
|
||
alphabet = me.get(_string_alphabet), | ||
base = alphabet.length, | ||
fractionalPart, | ||
integerPart, | ||
any = _string__empty, | ||
negative = false; | ||
|
||
value = +value; | ||
|
||
if (value < 0) { | ||
negative = true; | ||
value = -value; | ||
} | ||
|
||
integerPart = _floor(value); | ||
fractionalPart = String(value).split(_string__fullStop)[1]; | ||
|
||
do { | ||
any = alphabet.charAt(integerPart % base) + any; | ||
integerPart = _floor(integerPart / base); | ||
} while (integerPart); | ||
|
||
if (fractionalPart) { | ||
any += me.get(_string_radixPoint) + me.to(_class._reverse(fractionalPart)); | ||
} | ||
|
||
if (negative) { | ||
any = me.get(_string_minusSign) + any; | ||
} | ||
|
||
return any; | ||
} | ||
}, { | ||
ATTRS: { | ||
/** | ||
* The string of characters to use as single-digit numbers. The | ||
* length of this string determines the base of the result. Each | ||
* character should be unique within the string or else it will be | ||
* impossible to correctly convert a string back into a number. | ||
* Currently, non-BMP characters are not supported. | ||
* @attribute alphabet | ||
* @default '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~' | ||
* @type String | ||
*/ | ||
alphabet: { | ||
setter: function (value) { | ||
var lookup = {}, | ||
i = 0, | ||
length = value.length; | ||
|
||
for (; i < length; i += 1) { | ||
lookup[value.charAt(i)] = i; | ||
} | ||
|
||
this._set(_string_lookup, lookup); | ||
}, | ||
value: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~' | ||
}, | ||
/** | ||
* Used as a reverse lookup for a character index in alphabet. | ||
* @attribute lookup | ||
* @protected | ||
* @readOnly | ||
* @type Object | ||
*/ | ||
lookup: { | ||
readOnly: true, | ||
value: null | ||
}, | ||
/** | ||
* A single character string to prepend to negative values. This | ||
* character should not be in the alphabet. | ||
* Currently, non-BMP characters are not supported. | ||
* @attribute minusSign | ||
* @default '-' | ||
* @type String | ||
*/ | ||
minusSign: { | ||
value: '-' | ||
}, | ||
/** | ||
* A single character string to insert between the integer and | ||
* fractional parts of the number. This character should not be in | ||
* the alphabet. Currently, non-BMP characters are not supported. | ||
* @attribute radixPoint | ||
* @default '.' | ||
* @type String | ||
*/ | ||
radixPoint: { | ||
value: _string__fullStop | ||
} | ||
}, | ||
/** | ||
* A single character string to insert between the integer and | ||
* fractional parts of the number. This character should not be in | ||
* the alphabet. Currently, non-BMP characters are not supported. | ||
* @attribute radixPoint | ||
* @default '.' | ||
* @type String | ||
*/ | ||
radixPoint: { | ||
value: _string__fullStop | ||
* Reverse a string. | ||
* @method _reverse | ||
* @param {String} string | ||
* @protected | ||
* @return {String} reversedString | ||
* @static | ||
*/ | ||
_reverse: function (string) { | ||
return string.split(_string__empty).reverse().join(_string__empty); | ||
} | ||
} | ||
}); | ||
}(Y, arguments[1])); | ||
}); | ||
|
||
Y.AnyBaseConverter = _class; | ||
}(Y, NAME)); | ||
|
||
}, 'gallery-2012.06.20-20-07' ,{requires:['base'], skinnable:false}); | ||
}, 'gallery-2012.12.05-21-01', {"requires": ["base"]}); |
2 changes: 1 addition & 1 deletion
2
build/gallery-any-base-converter/gallery-any-base-converter-min.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.