Skip to content

Instantly share code, notes, and snippets.

@notiv-nt
Created November 21, 2015 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notiv-nt/237e32771e7ea0e167d3 to your computer and use it in GitHub Desktop.
Save notiv-nt/237e32771e7ea0e167d3 to your computer and use it in GitHub Desktop.
'use strict';
function List(integer, symbols) {
var defaults = {
integer: 0,
symbols: 'abcdefghiklmnopqrstvxyz0123456789'.split('')
}
// integer which used for + - / * ++ --
this.integer = integer || defaults.integer
// string of chars
this.chars = ''
// array of symbols
this.symbols = symbols
? symbols.split('')
: defaults.symbols
this.update()
}
List.prototype.toString = function() {
return this.chars;
}
List.prototype.valueOf = function() {
return this.integer;
}
List.prototype.update = function() {
var integer = this.integer,
notation = this.symbols.length,
arr = [],
val = '';
// check for empty
if (notation < 1)
return console.error(`Error: { Class List }: symbols are empty: [ ${ this.symbols } ]`)
while (integer >= 0) {
arr.push( integer % notation )
if (integer < notation) {
arr.push( integer )
break
}
integer = Math.floor( integer / notation )
}
// remove last and reverse
arr.pop()
arr.reverse()
arr.forEach( (v) => val += this.symbols[v] )
this.chars = val
}
List.prototype.set = function(int) {
if (typeof int === 'number') {
this.integer = int
this.update()
}
return this
}
List.prototype.get = function() {
return this.chars
}
List.prototype.inc = function() {
this.integer++
this.update()
return this
}
List.prototype.dec = function() {
this.integer = Math.max(0, this.integer - 1)
this.update()
return this
}
List.prototype.minus = function(int) {
this.integer = Math.max(0, this.integer - int)
this.update()
return this
}
List.prototype.plus = function(int) {
this.integer += int
this.update()
return this
}
List.prototype.div = function(int) {
this.integer = Math.max( 0, Math.floor( this.integer / int ) )
this.update()
return this
}
List.prototype.mult = function(int) {
this.integer *= int
this.update()
return this
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment