godprotocol
Version:
A distributed computing environment for Web 4.0 — integrating AI, decentralisation, and virtual computation.
205 lines (160 loc) • 5.34 kB
JavaScript
import Storage from "./functions/storage.js";
class Str extends Storage {
constructor(config, account) {
super(account)
this.config = config
this.type = 'string'
this.methods = {
...this.methods,
'title': {args: [], ret: ['Titled', 'string']},
'upper': {args: [], ret: ['CAPS', 'string']},
'lower': {args: [], ret: ['lower', 'string']},
'len': {args: [], ret: ['str.length', 'number']},
'slice': {args: [['start', 'number'], ['end', 'number']], ret: ['substr', 'string']},
'concat': {args: [['other', 'string']], ret: ['concatenated_str', 'string']},
'includes': {args: [['other', 'string']], ret: ['yes', 'boolean']},
'is_empty': {args: [], ret: ['yes', 'boolean']},
'is_blank': {args: [], ret: ['yes', 'boolean']},
'is_numeric': {args: [], ret: ['yes', 'boolean']},
'endswith': {args: [['char', 'string']], ret: ['yes', 'boolean']},
'startswith': {args: [['char', 'string']], ret: ['yes', 'boolean']},
'padend': {args: [['length', 'number'], ['char', 'string']], ret: ['result', 'string']},
'padstart': {args: [['length', 'number'], ['char', 'string']], ret: ['result', 'string']},
'count': {args: [['char', 'string']], ret: ['result', 'number']},
'reverse': {args: [], ret: ['reversed_string', 'string']},
'repeat': {args: [['count', 'number']], ret: ['repeated', 'string']},
'replace': {args: [['searchterm', 'string'], ['replacement', 'string'], ['globally', 'boolean']], ret: ['result', 'string']},
'charcode': {args: [['code', 'string']], ret: ['result', 'array']},
'find': {args: [['char', 'string'], ['last', 'boolean']], ret: ['index', 'number']},
'index': {args: [['index', 'number']], ret: ['char', 'string']},
'split': {args: [['char', 'string']], ret: ['result', 'array']},
'trim': {args: [['type', 'number', '0:boundaries', '1:beginning', '2:end'], ['char', 'string']], ret: ['result', 'string']},
}
}
title = async()=>{
let result = "";
this.value.split(' ').map(m=>{
result = `${result} ${m[0].toUpperCase()}${m.slice(1).toLowerCase()}`.trim()
})
return result
}
upper = ()=>{
return this.value.toUpperCase()
}
lower = ()=>{
return this.value.toLowerCase()
}
len = ()=> this.value.length
slice = async(start, args)=>{
let end
if(args.length > 1){
end = await args[1].literal()
}
return this.value.slice(start, end)
}
concat = (other)=>{
return `${this.value}${other}`
}
find = (other, args)=>{
let lst;
if (args.length>1){
lst = true;
}
return this.value[lst? 'lastIndexOf':'indexOf'](other)
}
includes = (other)=>this.value.includes(other)
index = other=>{
let result;
if (other < 0){
result = this.config.value.slice(other)[0]
}else result = this.config.value[other]
return result;
}
trim = async (other, args)=>{
let char;
if (args.length > 1){
char = await args[1].literal()
}else char = ' '
let esc_regx_char = (char) => char.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
let char_regx = esc_regx_char(char), result;
switch (other) {
case 1:
result = this.value.replace(new RegExp(`^${char_regx}+`), '');
break
case 2:
result = this.value.replace(new RegExp(`${char_regx}+$`), '');
break
case 0:
default:
result = this.value.replace(new RegExp(`^${char_regx}+|${char_regx}+$`, 'g'), '');
}
return result
}
is_empty = ()=>{
return !!!this.value
}
is_blank = ()=>{
return !!!this.value.trim()
}
is_numeric = ()=>{
return !!this.value && !isNaN(Number(this.value))
}
endswith = other=>{
return this.value.endsWith(other)
}
startswith = other=>{
return this.value.startsWith(other)
}
padend = async (other, args)=>{
let char = await args[1].literal()
return this.value.padEnd(other, char)
}
padstart = async (other, args)=>{
let char = await args[1].literal()
return this.value.padStart(other, char)
}
count = (other)=>{
let count = 0;
let position = 0, result;
while ((position = this.value.indexOf(other, position)) !== -1) {
count++;
position += other.length;
}
result = count;
return result
}
reverse = ()=>{
return this.value.split('').reverse().join('')
}
repeat = (count)=>{
return this.value.repeat(count)
}
replace = async(search_term, args)=>{
let globally = args[2], replacement = args[1] && await args[1].literal(), result;
result = this.value.replace(search_term, replacement)
if (globally){
while(result.includes(search_term)) result = result.replace(search_term, replacement)
}
return result
}
charcode = async(other)=>{
let code = other || "ascii", result;
let me = await this.literal()
if (code === "ascii") {
result = me.split("").map((char) => char.charCodeAt(0));
}
console.log(result, 'uh')
return result
}
split = async(other)=>{
let char = other || ''
let val = await this.literal();
let res = val.split(char)
console.log(res)
return res;
}
literal = async()=>{
return this.config.value
}
}
export default Str;