README
nv-string-basic
- nv-string-basic
- some basic string utils
- length ,number
- reusable-nonnest-string-template
install
- npm install nv-string-basic
usage
example
to_str
const {to_str} = require("nv-string-basic")
> to_str(undefined)
'undefined'
> to_str(null)
'null'
> to_str(123)
'123'
> to_str(123n)
'123n'
> to_str('abc')
'abc'
> to_str(Symbol('sym'))
'sym'
> console.log(to_str({a:100}))
{
"a": 100
}
undefined
> console.log(to_str([100,200,300]))
[
100,
200,
300
]
undefined
> class O {
[util.inspect.custom]() {
return('custom')
}
}
> var o = new O()
> to_str(o)
'custom'
>
> function tst() {return(999)}
undefined
> to_str(tst)
'function tst() {return(999)}'
>
pend string-array
> var ary = [
... 'ready',
... 'conding',
... 'opened',
... 'self_executing',
... 'resolved',
... 'self_rejected',
... 'bubble_rejected',
... 'self_paused',
... 'bubble_paused',
... 'impossible'
... ]
undefined
> x.ppend_fmt_sary(ary)
[
' ready',
' conding',
' opened',
' self_executing',
' resolved',
' self_rejected',
'bubble_rejected',
' self_paused',
' bubble_paused',
' impossible'
]
> x.apend_fmt_sary(ary)
[
'ready ',
'conding ',
'opened ',
'self_executing ',
'resolved ',
'self_rejected ',
'bubble_rejected',
'self_paused ',
'bubble_paused ',
'impossible '
]
>
stmpl and dtmpl
//first compile
var reuseable = stmpl`${0}-${1}-${2}-${`${3}`}`
> reuseable('x','y','z','nest2')
'x-y-z-nest2'
>
> reuseable('x','y')
'x-y--'
>
//first compile
var reuseable = stmpl`
<${0}>
<${1}></${1}>
<${1}></${1}>
<${1}></${1}>
<${1}></${1}>
</${0}>
`
var s = reusable('div','span')
<div>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
> console.log(reuseable('head','meta'))
<head>
<meta></meta>
<meta></meta>
<meta></meta>
<meta></meta>
</head>
undefined
>
//
var reusable = dtmpl`${'b'}-${'i'}-${'a'}-${`${'t'}`}`
reusable({
b:'before',
i:'in',
a:'after',
t:'tail'
})
>
'before-in-after-tail'
var reusable = dtmpl`
${'obj_open'}
${'ary_open'} 100,200,300,400 ${'ary_close'}
${'obj_close'}
`
var s = reusable({
obj_open: '【',
obj_close: '】',
ary_open:'<',
ary_close:'>'
})
> console.log(s)
【
< 100,200,300,400 >
】
>
var s = reusable({
obj_open: '<div>',
obj_close: '</div>',
ary_open:'<ul>',
ary_close:'</ul>'
})
> console.log(s)
<div>
<ul> 100,200,300,400 </ul>
</div>
>
var reusable = dtmpl`
${'open'}
${'open'} ${'close'}
${'open'} ${'close'}
${'open'} ${'close'}
${'close'}
`
var s = reusable({open:'<div>',close:'</div>'})
> console.log(s)
<div>
<div> </div>
<div> </div>
<div> </div>
</div>
>
var s = reusable({open:'<custom>',close:'</custom>'})
<custom>
<custom> </custom>
<custom> </custom>
<custom> </custom>
</custom>
/*
because string-literal-template is a runtime feature,
so stmpl and dtmpl NOT support nest ``,
to handle nest need to parse the template-source and implement the evaluate ,refer to nv-facutil-nest-string-tem,
its NOT convinient to use, USELESS,
*/
num
> const {prepend_num,append_num} = require("nv-string-basic")
undefined
> append_num(5,2,4)
'1010'
> prepend_num(5,2,4)
'0101'
>
loose parse_num
parse_num("1122334455667788998877665544332211n")
parse_num("-1122334455667788998877665544332211n")
parse_num("123")
parse_num("-123")
parse_num("0x1101")
parse_num("0b1101")
parse_num("0o1101")
parse_num("123.12")
parse_num("-123.12")
parse_num("123.12e3")
parse_num("-123.12e3")
parse_num("123.12e+3")
parse_num("-123.12e-3")
parse_num("2e0.5")
parse_num("-2e0.5")
> parse_num("1122334455667788998877665544332211n")
1122334455667788998877665544332211n
> parse_num("-1122334455667788998877665544332211n")
-1122334455667788998877665544332211n
>
> parse_num("123")
123
> parse_num("-123")
-123
>
> parse_num("0x1101")
4353
> parse_num("0b1101")
13
> parse_num("0o1101")
577
> parse_num("123.12")
123.12
> parse_num("-123.12e3")
-123120
>
> parse_num("123.12e3")
123120
> parse_num("12e2")
1200
> parse_num("123.12e+3")
123120
> parse_num("-123.12e-3")
-0.12312
>
>
> parse_num("2e0.5")
6.324555320336759 // 2 * (10 ** 0.5)
>
> parse_num("-2e0.5")
-6.324555320336759
>
> parse_num("+2e0.5")
6.324555320336759
> parse_num("-2e0.5")
-6.324555320336759
>
keep the type infomation
> NUM_TYPE_DICT
{
'6': 'Integer',
'7': 'Float',
'8': 'BigInt',
'9': 'PosInfinity',
'10': 'NegInfinity',
'11': 'NaN',
Integer: 6,
Float: 7,
BigInt: 8,
PosInfinity: 9,
NegInfinity: 10,
NaN: 11
}
> parse_num("1122334455667788998877665544332211n",true)
{ value: 1122334455667788998877665544332211n, type: 8 }
> parse_num("123",true)
{ value: 123, type: 6 }
> NUM_TYPE_DICT[6]
'Integer'
> parse_num("123.",true)
{ value: 123, type: 7 }
> NUM_TYPE_DICT[7]
'Float'
>
> parse_num("123e2",true)
{ value: 12300, type: 7 }
> NUM_TYPE_DICT[7]
'Float'
>
> parse_num("Infinity",true)
{ value: Infinity, type: 9 }
> NUM_TYPE_DICT[9]
'PosInfinity'
> parse_num("+Infinity",true)
{ value: Infinity, type: 9 }
> NUM_TYPE_DICT[9]
'PosInfinity'
> parse_num("-Infinity",true)
{ value: -Infinity, type: 10 }
> NUM_TYPE_DICT[9]
'PosInfinity'
> NUM_TYPE_DICT[10]
'NegInfinity'
> parse_num("NaN",true)
{ value: NaN, type: 11 }
> NUM_TYPE_DICT[11]
'NaN'
>
loose parse_int
parse_int("0b1101")
0b1101
parse_int("0o1101")
0o1101
parse_int("0x1101")
0x1101
parse_int("1101")
> parse_int("af") === undefined
> parse_int("0xaf")
175
>
/*
> parse_int("0b1101")
13
> 0b1101
13
> parse_int("0o1101")
577
> 0o1101
577
> parse_int("0x1101")
4353
> 0x1101
4353
> parse_int("1101")
1
>
*/
strict parse_float
//valid and same as parseFloat
parse_float("1")
parse_float("-1")
parse_float(".1")
parse_float("-.1")
parse_float("1.")
parse_float("-1.")
parse_float("-1.1")
parse_float("1.1")
parse_float("-1.1e2")
parse_float(".1E3")
/*
> parse_float("1")
1
> parse_float("-1")
-1
> parse_float(".1")
0.1
> parse_float("-.1")
-0.1
> parse_float("1.")
1
> parse_float("-1.")
-1
> parse_float("-1.1")
-1.1
> parse_float("1.1")
1.1
>
> parse_float("-1.1e2")
-110
>
> parse_float(".1E3")
100
*/
//valid but different with parseFloat
parse_float("1.1e")
parse_float("-1.1e")
parse_float("-1.1e2.5")
parse_float(".1E3.")
parse_float(".1E3.13")
/*
> parse_float("1.1e")
1.1
> parse_float("-1.1e")
-1.1
> parse_float("-1.1e2.5")
-347.85054261852173
>
> parse_float(".1E3.")
100
> parse_float(".1E3.13")
134.89628825916535
>
*/
//invalid
parse_float(".") === undefined
parse_float("1.2.") === undefined
parse_float("-.") === undefined
parse0 only parse undefined/null/boolean/number
> parse0('undefined')
undefined
> parse0('null')
null
> parse0('true')
true
> parse0('false')
false
> parse0('123')
123
> parse0('123e2')
12300
> parse0('Infinity')
Infinity
> parse0('-Infinity')
-Infinity
> parse0('-NaN')
NaN
> parse0('NaN')
NaN
> parse0('ssss')
ssss
>
reverse
var s = "abc"
> reverse(s)
'cba'
>
trim
var s = ' \t\r\na b c'
> trim_left(s)
'a b c'
>
var s = 'abcabc d e f abc'
> trim_left(s,"abc ")
'd e f abc'
>
> trim_right(s,"abc ")
'abcabc d e f'
>
> trim(s,"abc ")
'd e f'
>
> trim(s,["abcabc"," abc"])
' d e f'
>
var s = '我你 a b c 它'
trim(s,(ch)=>ch.codePointAt(0)>256 ||ch.codePointAt(0)===32 )
> trim(s,(ch)=>ch.codePointAt(0)>256 ||ch.codePointAt(0)===32 )
'a b c'
>
var s = '%$# abc !!@'
> trim_left(s,/^[^a-z]+/)
'abc !!@'
> trim_right(s,/[^a-z]+$/)
'%$# abc'
>
gen_split
> var g = gen_splits("abcd")
undefined
> Array.from(g)
[
[ 'abcd' ],
[ 'a', 'bcd' ],
[ 'ab', 'cd' ],
[ 'a', 'b', 'cd' ],
[ 'abc', 'd' ],
[ 'a', 'bc', 'd' ],
[ 'ab', 'c', 'd' ],
[ 'a', 'b', 'c', 'd' ]
]
> var g = gen_splits("abcd",2)
undefined
> Array.from(g)
[ [ 'a', 'bcd' ], [ 'ab', 'cd' ], [ 'abc', 'd' ] ]
>
u-string
var s = '𝑒';
> Array.from(s).length;
1
> s.length
2
>
> u(s);
'\\ud835\\udc52'
>
> from_u('\\ud835\\udc52')
'𝑒'
>
> u('A-𝑒-B')
'\\u0041\\u002d\\ud835\\udc52\\u002d\\u0042'
>
> from_u('\\u0041\\u002d\\ud835\\udc52\\u002d\\u0042')
'A-𝑒-B'
>
> u('A-𝑒-B',true)
'A-\\ud835\\udc52-B'
>
> from_u('A-\\ud835\\udc52-B')
'A-𝑒-B'
>
try_dequote, noquote if unnecessary
try_dequote(s,q='"',escc=['\b', '\f', '\n', '\r', '\t', '\x0B' ,' ','"','`',"'"])
> try_dequote('abc')
'abc'
> try_dequote('ab c')
'"ab c"'
> JSON.stringify('a\u0020bc')
'"a bc"'
>
> try_dequote('a\ud835bc')
'"a\\ud835bc"'
>
> try_dequote('a\xeebc')
'aîbc'
>
"operators"
//ONLY ADD is acurate, coz only string-add is monoid
> add('x','y')
'xy'
>
//OTHERS
> mul('abc',3)
'abcabcabc'
> mul(3,'abc')
'abcabcabc'
>
> sub('ABCxyzDEF','ABC')
'xyzDEF'
>
> sub('ABCxyzDEF','DEF')
'ABCxyz'
>
> length_div('abcdefg','abc')
[ 2, 1 ]
>
> length_quo('abcdefg','abc')
2
> length_mod('abcdefg','abc')
1
>
> div('abcabcabcab',3)
[ [ 'abc', 'abc', 'abc' ], 'ab' ]
>
> quo('abcabcabcab',3)
[ 'abc', 'abc', 'abc' ]
>
> mod('abcabcabcab',3)
'ab'
>
> sdiv('abcabcabcab','abc')
[ 3, 'ab' ]
>
> squo('abcabcabcab','abc')
3
>
> smod('abcabcabcab','abc')
'ab'
>
> sdiv('abcabcaXcab','abc')
[ null, null ]
>
API
to_str(o)
stmpl
tmpl
dtmpl
tmpl
prepend(s,width,pad=" ",force=false)
append(s,width,pad=" ",force=false)
prepend_num(n,radix,width,pad="0",force=false)
append_num(n,radix,width,pad="0",force=false)
is_bdigit_ch(ch)
is_bdigit_str(s)
is_odigit_ch(ch)
is_odigit_str(s)
is_ddigit_ch(ch)
is_ddigit_str(s)
is_hdigit_ch(ch)
is_hdigit_str(s)
is_ndigit_ch(n,ch) //n <=36
is_ndigit_str(n,s) //n <=36
slength(s)
blength(s)
u16length(s)
parse_num(s) //bigint-str int-str 0xaf 0b1001 0o555
is_jsint_str(s) //123 0xaf
parse_int(s) //123 0xaf 0b1001 0o555
is_int_str(s) //123 0xaf 0b1001 0o555
is_ary_idx_str(s) //is_int_str(s) && >=0 && < 2**32 -1
is_bigint_str(s)
is_float_str(s)
parse_float(s) //strict ".5.6" not valid,
//"." not valid
//"-." not valid,
//"-1.1e2.5" === -1.1 * (10 ** 2.5)
//others same as parseFloat
LOWER_CASE_ATOZ_STR
UPPER_CASE_ATOZ_STR
LOWER_CASE_ATOZ_MD
UPPER_CASE_ATOZ_MD
parse0(s,cfg= {only_value:true,with_value:true,with_type:false,unknown_as_string:false})
parse0.unknown
reverse(s)
trim_left(s,cond:String|RegExp|(ch)=>Boolean)
trim_right(s,cond:String|RegExp|(ch)=>Boolean)
trim(s,cond:String|RegExp|(ch)=>Boolean)
DFLT_TRIM_LEFT_REGEXP
DFLT_TRIM_RIGHT_REGEXP
DFLT_TRIM_REGEXP
gen_split_two(s)
gen_splits(s,n)
u(s,ignore_length_one=false)
from_u(s)
try_dequote(s,q='"',escc=['\b', '\f', '\n', '\r', '\t', '\x0B' ,' ','"','`',"'"])
mul(a,b) // Params[a:String,b:Int] OR Params[a:Int,b:String]
add(s0,s1)
sub(s0,s1)
length_div(s0,s1)
length_quo(s0,s1)
length_mod(s0,s1)
div(s,n:Int)
quo(s,n:Int)
mod(s,n:Int)
sdiv(s0,s1)
squo(s0,s1)
smod(s0,s1)
LICENSE
- ISC