npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

nv-string-basic

v1.0.56

Published

nv-string-basic =============== - nv-string-basic

Downloads

464

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 ]
    >

match_by

    > x.match_by('a','a')
    true
    > x.match_by('a',/a+/)
    true
    > x.match_by('a',(s)=>s==='a')
    true
    >

match

    > match('aaaXFbbxfuuu',/xf/gi)
    [
      [ 'a', 'a', 'a', matched: false ],
      [ 'X', 'F', matched: true ],
      [ 'b', 'b', matched: false ],
      [ 'x', 'f', matched: true ],
      [ 'u', 'u', 'u', matched: false ]
    ]
    >

sary_inseq_matchby sary_in_continuous_seq_matchby

    > x.sary_inseq_matchby(["a","abbb","asss","b","as"],[/ab+/,/as+/,/as/])
    true
    > x.sary_inseq_matchby(["a","abbb","asss","b","As"],[/ab+/,/as+/,/as/])
    false


    > x.sary_in_continuous_seq_matchby(["a","abbb","asss","b","as"],[/ab+/,/as+/,/as/])
    false
    > x.sary_in_continuous_seq_matchby(["a","abbb","asss","as","b"],[/ab+/,/as+/,/as/])
    true

_char_at

  • without validate

    > var s='A𝑒B'
    > _char_at(s,0)
    'A'
    > _char_at(s,1)            //high-surr  of high-low char
    '𝑒'
    > _char_at(s,2)            //low-surr   of high-low char
    '𝑒'
    > _char_at(s,3)
    'B'
    > s.length
    4
    >

API

  • to_str(o)

  • stmpltmpl

  • dtmpltmpl

  • 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)

  • match_by(s,str_or_regexp_or_cond_func_rtrn_bool)

  • match(s,regex)

  • _char_at(s,code_index)

LICENSE

  • ISC