-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
139 lines (139 loc) · 4.06 KB
/
main.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
var $s = s => new Spaghetti(s);
$s.strings = [];
class Spaghetti {
constructor(string, notpush) {
notpush = notpush || false;
this['[[PrimitiveValue]]'] = string;
if (!notpush) $s.strings.push(this);
}
*[Symbol.iterator]() {
for (let key in this.string) {
yield this.string.charAt(key);
}
}
get string() {
return this['[[PrimitiveValue]]'];
}
set string(v) {
this['[[PrimitiveValue]]'] = v;
}
get val() {
return this.string;
}
set val(d) {}
get len() {
return this.string.length;
}
reverse(d) {
this.string = this.string.split(d || '').reverse().join(d || '');
return this.string;
}
get reversed() {
return new Spaghetti(this.string).reverse();
}
stir() {
var a = [...this.string],
n = a.length;
for (var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
this.string = a.join("");
return this.string;
}
get stirred() {
return new Spaghetti(this.string).stir();
}
add(...strings) {
strings.forEach(string => {
if (typeof string == 'string') this.string += string;
else this.string += string.string;
return this.string;
});
}
remove(...strings) {
strings.forEach(i => this.string = this.string.replace(i, ''));
return this.string;
}
at(num) {
return this.string.charAt(num);
}
freeze() {
Object.defineProperty(this, 'string', {
writable: false
});
return this.string;
}
heat() {
Object.defineProperty(this, 'string', {
writable: true
});
return this.string;
}
array(splitter) {
return this.string.split(splitter || '')
}
has(s) {
return this.string.indexOf(s) > -1
}
replace(s, t) {
this.string = this.string.replace(s, t);
return this.string;
}
replaceAt(n, t) {
this.string = this.string.substr(0, n) + t + this.string.substr(n + t.length);
return this.string;
}
removeAt(n) {
this.string = n == 0 ? this.string.slice(1) : str.slice(0, index - 1) + str.slice(index);
return this.string;
}
cook(password, oven_heat_level) {
var arr = [];
password.split('').forEach(e => arr.push(e.charCodeAt(0) * oven_heat_level));
var seed = arr,
string = [...this.string],
arr = [];
string.forEach((e, i) => arr.push(String.fromCharCode(e.charCodeAt(0) + seed[i % seed.length])));
if (oven_heat_level > 1e+13) console.warn('WARNING: The requested heat level is very high. You may have burnt your spaghetti string. Proceed with caution.');
this.string = arr.join('');
return this.string;
}
uncook(password, oven_heat_level) {
var arr = [];
password.split('').forEach(e => arr.push(e.charCodeAt(0) * oven_heat_level));
var seed = arr,
string = [...this.string],
arr = [];
string.forEach((e, i) => arr.push(String.fromCharCode(e.charCodeAt(0) - seed[i % seed.length])));
this.string = arr.join('');
return this.string;
}
copy() {
return new Spaghetti(this.string, true)
}
map(f) {
this.string = [...this.string].map(f).join('');
return this.string;
}
each(f) {
[...this.string].forEach(f);
return this.string;
}
sort(f) {
this.string = [...this.string].sort(f).join('');
return this.string
}
hasAt(s, n) {
return this.string.charAt(n) == s
}
addAt(s, n) {
this.string = this.string.slice(0, n) + s + this.string.slice(n);
return this.string;
}
}
Spaghetti.prototype.valueOf = function() {return this.string};
$s.alphabet = $s('abcdefghijklmnopqrstuvwxyz');
$s.random = n => $s([...Array(n)].map(i => (~~(Math.random() * 36)).toString(36)).join(''));