2023-08-27 18:21:25 +00:00
|
|
|
define("values/string", () => {
|
|
|
|
var String = env.global.String = function(this: String | undefined, arg: any) {
|
|
|
|
var val;
|
|
|
|
if (arguments.length === 0) val = '';
|
|
|
|
else val = arg + '';
|
|
|
|
if (this === undefined || this === null) return val;
|
|
|
|
else (this as any).value = val;
|
|
|
|
} as StringConstructor;
|
|
|
|
|
2023-09-04 11:30:57 +00:00
|
|
|
env.setProto('string', String.prototype);
|
|
|
|
setConstr(String.prototype, String);
|
2023-08-27 18:21:25 +00:00
|
|
|
|
2023-09-04 11:30:57 +00:00
|
|
|
setProps(String.prototype, {
|
2023-08-27 18:21:25 +00:00
|
|
|
toString() {
|
|
|
|
if (typeof this === 'string') return this;
|
|
|
|
else return (this as any).value;
|
|
|
|
},
|
|
|
|
valueOf() {
|
|
|
|
if (typeof this === 'string') return this;
|
|
|
|
else return (this as any).value;
|
|
|
|
},
|
|
|
|
|
|
|
|
substring(start, end) {
|
|
|
|
if (typeof this !== 'string') {
|
|
|
|
if (this instanceof String) return (this as any).value.substring(start, end);
|
|
|
|
else throw new Error('This function may be used only with primitive or object strings.');
|
|
|
|
}
|
|
|
|
start = start ?? 0 | 0;
|
|
|
|
end = (end ?? this.length) | 0;
|
2023-09-04 11:30:57 +00:00
|
|
|
|
|
|
|
const res = [];
|
|
|
|
|
|
|
|
for (let i = start; i < end; i++) {
|
|
|
|
if (i >= 0 && i < this.length) res[res.length] = this[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return internals.stringFromStrings(res);
|
2023-08-27 18:21:25 +00:00
|
|
|
},
|
|
|
|
substr(start, length) {
|
|
|
|
start = start ?? 0 | 0;
|
|
|
|
|
|
|
|
if (start >= this.length) start = this.length - 1;
|
|
|
|
if (start < 0) start = 0;
|
|
|
|
|
|
|
|
length = (length ?? this.length - start) | 0;
|
2023-09-04 11:30:57 +00:00
|
|
|
const end = length + start;
|
|
|
|
const res = [];
|
|
|
|
|
|
|
|
for (let i = start; i < end; i++) {
|
|
|
|
if (i >= 0 && i < this.length) res[res.length] = this[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return internals.stringFromStrings(res);
|
2023-08-27 18:21:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
toLowerCase() {
|
2023-09-04 11:30:57 +00:00
|
|
|
// TODO: Implement localization
|
|
|
|
const res = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < this.length; i++) {
|
|
|
|
const c = internals.char(this[i]);
|
|
|
|
|
|
|
|
if (c >= 65 && c <= 90) res[i] = c - 65 + 97;
|
|
|
|
else res[i] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return internals.stringFromChars(res);
|
2023-08-27 18:21:25 +00:00
|
|
|
},
|
|
|
|
toUpperCase() {
|
2023-09-04 11:30:57 +00:00
|
|
|
// TODO: Implement localization
|
|
|
|
const res = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < this.length; i++) {
|
|
|
|
const c = internals.char(this[i]);
|
|
|
|
|
|
|
|
if (c >= 97 && c <= 122) res[i] = c - 97 + 65;
|
|
|
|
else res[i] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return internals.stringFromChars(res);
|
2023-08-27 18:21:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
charAt(pos) {
|
|
|
|
if (typeof this !== 'string') {
|
|
|
|
if (this instanceof String) return (this as any).value.charAt(pos);
|
|
|
|
else throw new Error('This function may be used only with primitive or object strings.');
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = pos | 0;
|
|
|
|
if (pos < 0 || pos >= this.length) return '';
|
|
|
|
return this[pos];
|
|
|
|
},
|
|
|
|
charCodeAt(pos) {
|
2023-09-04 11:30:57 +00:00
|
|
|
if (typeof this !== 'string') {
|
|
|
|
if (this instanceof String) return (this as any).value.charAt(pos);
|
|
|
|
else throw new Error('This function may be used only with primitive or object strings.');
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = pos | 0;
|
|
|
|
if (pos < 0 || pos >= this.length) return 0 / 0;
|
|
|
|
return internals.char(this[pos]);
|
2023-08-27 18:21:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
startsWith(term, pos) {
|
|
|
|
if (typeof this !== 'string') {
|
|
|
|
if (this instanceof String) return (this as any).value.startsWith(term, pos);
|
|
|
|
else throw new Error('This function may be used only with primitive or object strings.');
|
|
|
|
}
|
|
|
|
pos = pos! | 0;
|
2023-09-04 11:30:57 +00:00
|
|
|
term = term + "";
|
|
|
|
|
|
|
|
if (pos < 0 || this.length < term.length + pos) return false;
|
|
|
|
|
|
|
|
for (let i = 0; i < term.length; i++) {
|
|
|
|
if (this[i + pos] !== term[i]) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2023-08-27 18:21:25 +00:00
|
|
|
},
|
|
|
|
endsWith(term, pos) {
|
|
|
|
if (typeof this !== 'string') {
|
|
|
|
if (this instanceof String) return (this as any).value.endsWith(term, pos);
|
|
|
|
else throw new Error('This function may be used only with primitive or object strings.');
|
|
|
|
}
|
|
|
|
pos = (pos ?? this.length) | 0;
|
2023-09-04 11:30:57 +00:00
|
|
|
term = term + "";
|
|
|
|
|
|
|
|
const start = pos - term.length;
|
|
|
|
|
|
|
|
if (start < 0 || this.length < term.length + start) return false;
|
|
|
|
|
|
|
|
for (let i = 0; i < term.length; i++) {
|
|
|
|
if (this[i + start] !== term[i]) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2023-08-27 18:21:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
indexOf(term: any, start) {
|
|
|
|
if (typeof this !== 'string') {
|
|
|
|
if (this instanceof String) return (this as any).value.indexOf(term, start);
|
|
|
|
else throw new Error('This function may be used only with primitive or object strings.');
|
|
|
|
}
|
|
|
|
|
2023-09-09 14:32:46 +00:00
|
|
|
if (typeof term[env.global.Symbol.search] !== 'function') term = RegExp.escape(term);
|
2023-08-27 18:21:25 +00:00
|
|
|
|
2023-09-09 14:32:46 +00:00
|
|
|
return term[env.global.Symbol.search](this, false, start);
|
2023-08-27 18:21:25 +00:00
|
|
|
},
|
|
|
|
lastIndexOf(term: any, start) {
|
|
|
|
if (typeof this !== 'string') {
|
|
|
|
if (this instanceof String) return (this as any).value.indexOf(term, start);
|
|
|
|
else throw new Error('This function may be used only with primitive or object strings.');
|
|
|
|
}
|
|
|
|
|
2023-09-09 14:32:46 +00:00
|
|
|
if (typeof term[env.global.Symbol.search] !== 'function') term = RegExp.escape(term);
|
2023-08-27 18:21:25 +00:00
|
|
|
|
2023-09-09 14:32:46 +00:00
|
|
|
return term[env.global.Symbol.search](this, true, start);
|
2023-08-27 18:21:25 +00:00
|
|
|
},
|
|
|
|
includes(term, start) {
|
|
|
|
return this.indexOf(term, start) >= 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
replace(pattern: any, val) {
|
|
|
|
if (typeof this !== 'string') {
|
|
|
|
if (this instanceof String) return (this as any).value.replace(pattern, val);
|
|
|
|
else throw new Error('This function may be used only with primitive or object strings.');
|
|
|
|
}
|
|
|
|
|
2023-09-09 14:32:46 +00:00
|
|
|
if (typeof pattern[env.global.Symbol.replace] !== 'function') pattern = RegExp.escape(pattern);
|
2023-08-27 18:21:25 +00:00
|
|
|
|
2023-09-09 14:32:46 +00:00
|
|
|
return pattern[env.global.Symbol.replace](this, val);
|
2023-08-27 18:21:25 +00:00
|
|
|
},
|
|
|
|
replaceAll(pattern: any, val) {
|
|
|
|
if (typeof this !== 'string') {
|
|
|
|
if (this instanceof String) return (this as any).value.replace(pattern, val);
|
|
|
|
else throw new Error('This function may be used only with primitive or object strings.');
|
|
|
|
}
|
|
|
|
|
2023-09-09 14:32:46 +00:00
|
|
|
if (typeof pattern[env.global.Symbol.replace] !== 'function') pattern = RegExp.escape(pattern, "g");
|
2023-08-27 18:21:25 +00:00
|
|
|
if (pattern instanceof RegExp && !pattern.global) pattern = new pattern.constructor(pattern.source, pattern.flags + "g");
|
|
|
|
|
2023-09-09 14:32:46 +00:00
|
|
|
return pattern[env.global.Symbol.replace](this, val);
|
2023-08-27 18:21:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
match(pattern: any) {
|
|
|
|
if (typeof this !== 'string') {
|
|
|
|
if (this instanceof String) return (this as any).value.match(pattern);
|
|
|
|
else throw new Error('This function may be used only with primitive or object strings.');
|
|
|
|
}
|
|
|
|
|
2023-09-09 14:32:46 +00:00
|
|
|
if (typeof pattern[env.global.Symbol.match] !== 'function') pattern = RegExp.escape(pattern);
|
2023-08-27 18:21:25 +00:00
|
|
|
|
2023-09-09 14:32:46 +00:00
|
|
|
return pattern[env.global.Symbol.match](this);
|
2023-08-27 18:21:25 +00:00
|
|
|
},
|
|
|
|
matchAll(pattern: any) {
|
|
|
|
if (typeof this !== 'string') {
|
|
|
|
if (this instanceof String) return (this as any).value.matchAll(pattern);
|
|
|
|
else throw new Error('This function may be used only with primitive or object strings.');
|
|
|
|
}
|
|
|
|
|
2023-09-09 14:32:46 +00:00
|
|
|
if (typeof pattern[env.global.Symbol.match] !== 'function') pattern = RegExp.escape(pattern, "g");
|
2023-08-27 18:21:25 +00:00
|
|
|
if (pattern instanceof RegExp && !pattern.global) pattern = new pattern.constructor(pattern.source, pattern.flags + "g");
|
|
|
|
|
2023-09-09 14:32:46 +00:00
|
|
|
return pattern[env.global.Symbol.match](this);
|
2023-08-27 18:21:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
split(pattern: any, lim, sensible) {
|
|
|
|
if (typeof this !== 'string') {
|
|
|
|
if (this instanceof String) return (this as any).value.split(pattern, lim, sensible);
|
|
|
|
else throw new Error('This function may be used only with primitive or object strings.');
|
|
|
|
}
|
|
|
|
|
2023-09-09 14:32:46 +00:00
|
|
|
if (typeof pattern[env.global.Symbol.split] !== 'function') pattern = RegExp.escape(pattern, "g");
|
2023-08-27 18:21:25 +00:00
|
|
|
|
2023-09-09 14:32:46 +00:00
|
|
|
return pattern[env.global.Symbol.split](this, lim, sensible);
|
2023-08-27 18:21:25 +00:00
|
|
|
},
|
|
|
|
slice(start, end) {
|
|
|
|
if (typeof this !== 'string') {
|
|
|
|
if (this instanceof String) return (this as any).value.slice(start, end);
|
|
|
|
else throw new Error('This function may be used only with primitive or object strings.');
|
|
|
|
}
|
|
|
|
|
|
|
|
start = wrapI(this.length, start ?? 0 | 0);
|
|
|
|
end = wrapI(this.length, end ?? this.length | 0);
|
|
|
|
|
|
|
|
if (start > end) return '';
|
|
|
|
|
|
|
|
return this.substring(start, end);
|
|
|
|
},
|
|
|
|
|
|
|
|
concat(...args) {
|
|
|
|
if (typeof this !== 'string') {
|
|
|
|
if (this instanceof String) return (this as any).value.concat(...args);
|
|
|
|
else throw new Error('This function may be used only with primitive or object strings.');
|
|
|
|
}
|
|
|
|
|
|
|
|
var res = this;
|
|
|
|
for (var arg of args) res += arg;
|
|
|
|
return res;
|
|
|
|
},
|
|
|
|
|
|
|
|
trim() {
|
|
|
|
return this
|
|
|
|
.replace(/^\s+/g, '')
|
|
|
|
.replace(/\s+$/g, '');
|
2023-08-05 15:37:18 +00:00
|
|
|
}
|
2023-08-27 18:21:25 +00:00
|
|
|
});
|
|
|
|
|
2023-09-04 11:30:57 +00:00
|
|
|
setProps(String, {
|
2023-08-27 18:21:25 +00:00
|
|
|
fromCharCode(val) {
|
2023-09-04 11:30:57 +00:00
|
|
|
return internals.stringFromChars([val | 0]);
|
2023-08-27 18:21:25 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
env.global.Object.defineProperty(String.prototype, 'length', {
|
|
|
|
get() {
|
|
|
|
if (typeof this !== 'string') {
|
|
|
|
if (this instanceof String) return (this as any).value.length;
|
|
|
|
else throw new Error('This function may be used only with primitive or object strings.');
|
|
|
|
}
|
|
|
|
|
2023-09-04 11:30:57 +00:00
|
|
|
return internals.strlen(this);
|
2023-08-27 18:21:25 +00:00
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: false,
|
|
|
|
});
|
|
|
|
});
|