-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertable-string.js
148 lines (141 loc) · 4.34 KB
/
convertable-string.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict';
module.exports = class ConvertableString {
constructor() {
return ((self) => Object.create({
from: function(source) {
if (!!~['undefined', 'boolean', 'function'].indexOf(typeof source))
throw new Error('Source should be type string, number or object');
if (!source.string)
this[({
'string': 'string',
'number': 'position',
'object': 'codes'
})[typeof source]] = source;
else
this.position = source.position;
return this.string;
}
}, {
ALPHABET: {
value: typeof arguments[0] === 'string' && Object.keys(arguments).length === 1
? arguments[0] : self.getAlphabet(arguments)
},
string: {
get() {
if (!(this._string = this._string || ''))
if (this._position)
this.string = self.stringAt.call(this, this._position);
else if (this._codes && this._codes.length)
this.string = self.stringBy.call(this, this._codes);
return this._string;
},
set(value) {
self.setProperties.call(this, { string: ''+value });
}
},
position: {
get() {
if (!(this._position = this._position || 0))
if (this._string)
this.position = self.positionOf.call(this, this._string);
else if (this._codes && this._codes.length)
this.position = self.positionOf.call(this,
self.stringBy.call(this, this._codes));
return this._position;
},
set(value) {
self.setProperties.call(this, { position: +value });
}
},
codes: {
get() {
if (!(this._codes = this._codes || []).length)
if (this._string)
this.codes = self.codesIn.call(this, this._string);
else if (this._position)
this.codes = self.codesIn.call(this, self.stringAt.call(this,
this._position));
return this._codes;
},
set(value) {
self.setProperties.call(this, { codes: [].concat(value) });
}
}
}))(this);
}
setProperties(props) {
Object.defineProperties(this, {
_string: {
value: props.string || '',
writable: true,
enumerable: false
},
_position: {
value: props.position || 0,
writable: true,
enumerable: false
},
_codes: {
value: props.codes || [],
writable: true,
enumerable: false
}
});
}
positionOf(string = '') {
let exps = Array.from(Array(string.length), (x,i) => i).reverse();
let code = 0, exp = 0;
return string.split('').reduce((total, letter, i) => {
code = this.ALPHABET.indexOf(letter) + 1;
exp = Math.pow(this.ALPHABET.length, exps[i]);
total += code * exp;
return total;
}, 0);
}
codesIn(string = '') {
return string.split('').reduce((codes, letter) =>
codes.push(this.ALPHABET.indexOf(letter) + 1) && codes
, []);
}
stringAt(position = 0) {
let string = '';
let search = (position = 0) => {
if (position <= this.ALPHABET.length) {
string += this.ALPHABET[position-1];
return;
}
let min = 0, max = 0, step = 0, exp = 1, next = 0, tmp = 0;
while ((max += tmp = Math.pow(this.ALPHABET.length, exp)) < position) {
step = tmp;
exp++;
}
min = max / this.ALPHABET.length;
tmp = Math.floor((position - min) / step);
next = position - step * (tmp + 1);
string += this.ALPHABET[tmp];
search(next);
};
search(position);
return string;
}
stringBy(codes = []) {
return codes.reduce((str, code) => (str += this.ALPHABET[code-1]) && str, '');
}
/**
* Get current alphabet as string
* For example:
* pass 97, 26 to get a-z
* 65, 26 returns A-Z
* 48, 10 returns 0-9
* by default (without params) it returns printable ascii characters
* @return {String}
*/
getAlphabet() {
let [ start = 0, length = 0 ] = arguments[0];
let excluded = Array(32).fill().map((n,i) => i).concat(127);
return Array(length || 128).fill().map((n,i) => start+i).reduce((chars,i) =>
!start && !length && !!~excluded.indexOf(i)
? chars : (chars += String.fromCharCode(i)) && chars
, '');
}
};