str_slice function
This commit is contained in:
parent
cba4669d54
commit
581228ada9
3 changed files with 54 additions and 2 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "asajs",
|
"name": "asajs",
|
||||||
"version": "4.1.1",
|
"version": "4.1.2-indev",
|
||||||
"description": "Create your Minecraft JSON-UI resource packs using JavaScript",
|
"description": "Create your Minecraft JSON-UI resource packs using JavaScript",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"Minecraft",
|
"Minecraft",
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,14 @@ export function isHasBinding(input: string) {
|
||||||
return /#\w+/.test(input)
|
return /#\w+/.test(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isBinding(input: string) {
|
||||||
|
return /^#\w+$/.test(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isNumber(input: string) {
|
||||||
|
return /^[+-]?(?:\d+|\d+\.\d*|\.\d+)(?:[eE][+-]?\d+)?$/.test(input)
|
||||||
|
}
|
||||||
|
|
||||||
export function isString(input: string) {
|
export function isString(input: string) {
|
||||||
return /^'.+'$/.test(input)
|
return /^'.+'$/.test(input)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { RandomBindingString, RandomString, ResolveBinding } from "../../components/Utils.js"
|
import { RandomBindingString, RandomString, ResolveBinding } from "../../components/Utils.js"
|
||||||
import { BindingItem } from "../../types/properties/value.js"
|
import { BindingItem } from "../../types/properties/value.js"
|
||||||
import { bindingFuntions } from "../Configuration.js"
|
import { bindingFuntions } from "../Configuration.js"
|
||||||
import { isString } from "./Checker.js"
|
import { isBinding, isNumber, isString } from "./Checker.js"
|
||||||
import { Expression, GenBinding } from "./types.js"
|
import { Expression, GenBinding } from "./types.js"
|
||||||
|
|
||||||
type CallbackRet = {
|
type CallbackRet = {
|
||||||
|
|
@ -141,6 +141,12 @@ export const defaultFunctions = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
not_contains: (source_str, contains_str) => {
|
||||||
|
return {
|
||||||
|
value: `(${source_str} - ${contains_str}) = ${source_str}`,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
contains: (source_str, contains_str) => {
|
contains: (source_str, contains_str) => {
|
||||||
return {
|
return {
|
||||||
value: `not ((${source_str} - ${contains_str}) = ${source_str})`,
|
value: `not ((${source_str} - ${contains_str}) = ${source_str})`,
|
||||||
|
|
@ -195,6 +201,44 @@ export const defaultFunctions = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
str_slice: (str, start, end) => {
|
||||||
|
const prefix = `'asajs:${RandomString(5)}:'`
|
||||||
|
|
||||||
|
if (isBinding(start)) start = `('%.' + (${start} + ${prefix.length - 2}) + 's')`
|
||||||
|
else if (isNumber(start)) start = `'%.${+start + prefix.length - 2}s'`
|
||||||
|
else throw new Error("Invalid start")
|
||||||
|
|
||||||
|
const genStrBinds: GenBinding = {
|
||||||
|
source: ``,
|
||||||
|
target: RandomBindingString(),
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isBinding(str)) genStrBinds.source = `(${prefix} + ${str})`
|
||||||
|
else if (isString(str)) genStrBinds.source = `${prefix.slice(0, -1)}${str.slice(1)}`
|
||||||
|
else throw new Error("Invalid str")
|
||||||
|
|
||||||
|
if (end) {
|
||||||
|
if (isBinding(end)) end = `('%.' + (${end} + ${prefix.length - 2}) + 's')`
|
||||||
|
else if (isNumber(end)) end = `'%.${+end + prefix.length - 2}s'`
|
||||||
|
else throw new Error("Invalid end")
|
||||||
|
|
||||||
|
const sliceEnd: GenBinding = {
|
||||||
|
source: `(${end} * ${genStrBinds.target})`,
|
||||||
|
target: RandomBindingString(),
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
genBindings: [genStrBinds, sliceEnd],
|
||||||
|
value: `${sliceEnd.target} - (${start} * ${sliceEnd.target})`,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
genBindings: [genStrBinds],
|
||||||
|
value: `${genStrBinds.target} - (${start} * ${genStrBinds.target})`,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a translatable string
|
* Return a translatable string
|
||||||
* @param key
|
* @param key
|
||||||
|
|
|
||||||
Reference in a new issue