JSPM

  • Created
  • Published
  • Downloads 19
  • Score
    100M100P100Q8674F
  • License ISC

nv-facutil-basic =============== - nv-facutil-basic

Package Exports

  • nv-facutil-basic

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nv-facutil-basic) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

nv-facutil-basic

  • nv-facutil-basic

install

  • npm install nv-facutil-basic

usage

example

const fac_bsc = require("nv-facutil-basic");

> fac_bsc.is_node()
true
>
> var d = {a:{b:{c:200}}}

> fac_bsc.optional_chaining(d,'a','b')
{ c: 200 }
> fac_bsc.optional_chaining(d,'a','b','e','c')
undefined
>
> fac_bsc.nullish(null,200)
200
> fac_bsc.nullish(undefined,200)
200
> fac_bsc.nullish(false,200)
false
> fac_bsc.nullish(true,200)
true
>

class Cls {
    constructor(){
        this.tag="div"
    }
}

function repr(that,stag,etag) {
    return(stag+that.tag+etag)
}

fac_bsc.add_repr(Cls,repr,'<','>')
> var o = new Cls()
undefined
> o
<div>
>


> var s = `
... aaa
... bbb
... ccc
... `
>

> s
'\naaa\nbbb\nccc\n'
>


> console.log(fac_bsc.indent(s,2))

        aaa
        bbb
        ccc


> console.log(fac_bsc.indent(s,2,"\t"))

                aaa
                bbb
                ccc

> console.log(fac_bsc.indent(s,3,"@"))
@@@
@@@aaa
@@@bbb
@@@ccc
@@@

>


> class SomeCls {}
undefined
> fac_bsc.add_string_tag(SomeCls,that=>'kkk')
undefined
> var o = new SomeCls()
undefined
> o
SomeCls [kkk] {}
>


> fac_bsc.creat_id()
'5055a812-2b24-4c9e-ab11-87088e51203c'
> fac_bsc.get_abbr('5055a812-2b24-4c9e-ab11-87088e51203c')
'5055a812'
>

fac_bsc.creat_reserved(['EPSILON','ε'],['LAMBDA','λ'])

> EPSILON
'ε'
> LAMBDA
'λ'
> Object.getOwnPropertyDescriptors(global).EPSILON
{ value: 'ε', writable: false, enumerable: false, configurable: false }
> Object.getOwnPropertyDescriptors(global).LAMBDA
{ value: 'λ', writable: false, enumerable: false, configurable: false }
>

iflet

var a = 3

var x = iflet(
    a<=1,100,
    a<=2,200,
    300
)

>300

forof

> var o = {a:100,b:200}
> for(let e of forof(o)) {console.log(e)}
[ 100,'a' ]
[ 200,'b' ]

forlst

> x.forlst([10,20,30,40],(r,i)=>console.log(r,i))
40 3
30 2
20 1
10 0
undefined
>

mirror

> fac_bsc.mirr([11,21,31])
SimpleMirr(6) [Map] {
  0 => 11,
  11 => 0,
  1 => 21,
  21 => 1,
  2 => 31,
  31 => 2
}
> fac_bsc.mirr({a:11,b:21,c:31})
SimpleMirr(6) [Map] {
  'a' => 11,
  11 => 'a',
  'b' => 21,
  21 => 'b',
  'c' => 31,
  31 => 'c'
}
>

json

var ary=[[1,2],3,[4,[[6]]]]
jrplc(d,(r)=>r*2)
console.log(ary)
//[[2,4],6,[8,[[12]]]]

var d = {1:100,x:{2:{3:300}}}
jrplc(d,(r,i,k)=>r+k)
console.log(d)
//{ '1': '1001', x: { '2': { '3': '3003' } } }

API

  • fac_bsc.is_node() //if the runtime is nodejs

  • fac_bsc.add_repr(Cls,repr_func,...other_repr_func_args) //[util.inspect.custom] in nodejs , else .prototype.repr

  • fac_bsc.add_string_tag(Cls,repr_func,...other_repr_func_args) //get [Symbol.toStringTag]() {}

  • fac_bsc.optional_chaining(obj,...keys) //same as ?., for api-compatible

  • fac_bsc.nullish(a,b); // same as ?? ,for api-compatible

  • fac_bsc.nc(a,b); //same as nullish, for api-compatible

  • fac_bsc.is_null(o);

  • fac_bsc.is_undefined(o);

  • fac_bsc.is_nullish(o); //null or undefined

  • fac_bsc.is_nothing(o); //Nil None nil nullptr NULL empty hole, refer to CONSTS

  • fac_bsc.is_assignable(o); //o !== noexist

  • fac_bsc.is_symbol(o)

  • fac_bsc.is_bool(o)

  • fac_bsc.is_num(o)

  • fac_bsc.is_float(o)

  • fac_bsc.is_int(o)

  • fac_bsc.is_nan(o)

  • fac_bsc.is_infinity(o)

  • fac_bsc.is_abnormal_num(o)

  • fac_bsc.is_normal_num(o)

  • fac_bsc.is_str(o)

  • fac_bsc.is_prim(o)

  • fac_bsc.is_bigint(o)

  • fac_bsc.is_ary(o)

  • fac_bsc.is_u8ary(o)

  • fac_bsc.is_buf(o) //buffer is same as u8ary

  • fac_bsc.is_u8cary(o) //clamped uint8-array

  • fac_bsc.is_u16ary(o)

  • fac_bsc.is_u32ary(o)

  • fac_bsc.is_i8ary(o)

  • fac_bsc.is_i16ary(o)

  • fac_bsc.is_i32ary(o)

  • fac_bsc.is_f32ary(o)

  • fac_bsc.is_f64ary(o)

  • fac_bsc.is_bu64ary(o)

  • fac_bsc.is_bi64ary(o)

  • fac_bsc.is_typed_ary(o)

  • fac_bsc.is_set(o)

  • fac_bsc.is_map(o)

  • fac_bsc.is_wkset(o)

  • fac_bsc.is_wkmap(o)

  • fac_bsc.is_wkref(o)

  • fac_bsc.is_weak(o)

  • fac_bsc.is_dict(o) //object NOT array|typed-array|weak|set|map|cls|func|promise|proxy| // regex|date|data-view|generator

  • fac_bsc.is_normal_dict(o) //dict but NOT promise| proxy | generator

  • fac_bsc.is_promise(o)

  • fac_bsc.is_proxy(o) //only work in nodejs

  • fac_bsc.is_sync_generator(o)

  • fac_bsc.is_async_generator(o)

  • fac_bsc.is_generator(o)

  • fac_bsc.is_ppg(o) //promise| proxy | generator

  • fac_bsc.is_regex(o)

  • fac_bsc.is_cls_or_func(o)

  • fac_bsc.is_cls(o)

  • fac_bsc.is_async_func(o)

  • fac_bsc.is_sync_gen(o)

  • fac_bsc.is_async_gen(o)

  • fac_bsc.is_gen(o) //sync_gen or async_gen

  • fac_bsc.is_normal_func(o) //func but NOT gen | async_func

  • fac_bsc.is_lambda(o) //()=>{} or async()=>{}

  • fac_bsc.is_func(o) //

  • fac_bsc.is_date(o)

  • fac_bsc.is_dv(o) //dataview

  • fac_bsc.is_arybuf(o) //array-buf

  • fac_bsc.is_enum_iter_leaf(o) //when typed-array |array |map |set: return true if size/length === 0 //when object : return true if Object.keys(o).length === 0 //others return false

  • fac_bsc.is_falsy(o) // '',0,0n,[],{},and empty Set/Map and empty typed-array

  • fac_bsc.is_truthy(o)

  • fac_get_dtype(o) //return a string of data-type //refer to CONSTS DTYPE_NUM

  • fac_get_dtype_num(o) //return a big-int number of data-type

  • fac_bsc.iflet(cond,rslt,cond,rslt,....else)

  • fac_bsc.forof(o) //if o has [Symbol.iterator] return o, //else try for-in and return a generator //for api-compatible and consistency

  • fac_bsc.creat_reserved(...kvpairs) //creat global reserved variables, similar to usage of undefined

  • fac_bsc.creat_id() // if nodejs call crypto.randomUUID, else using nanoid //nanoid is slightly faster than uuidv4

  • fac_bsc.get_abbr(id) // the first-8 of a uuid

  • fac_bsc.creat_bigint_id() //convert uuid to a bigint

  • fac_bsc.creat_nodash_id() //remove dash in uuid

  • fac_bsc.creat_4int_id_ary() //convert uuid to 4-int-array

  • fac_bsc.SNidPool //a int or bigint ID, //useful when using adj-table as data structure
    //similiar with SNidPool,used in fixed-size scenario; //these are used in fixed-size array-pool ,
    // useful when using sharedArrayBuffer,otherwise useless

  • fac_bsc.AidPool //(Array);

  • fac_bsc.T8AidPool //(Uint8Array);

  • fac_bsc.T16AidPool //(Uint16Array);

  • fac_bsc.T32AidPool //(Uint32Array);

  • fac_bsc.T64AidPool //(BigUint64Array);

  • fac_bsc.jdcp(jsond) //JSON.parse(JSON.stringify(jsond))

  • fac_bsc.jrplc(jsond,f) // f: (val,index,key,parent) => {...}

  • fac_bsc.jmap(jsond,f) // f: (val,index,key,parent) => {...}

  • fac_bsc.unescq(s) //JSON.parse('"'+s+'"')

  • fac_bsc.typof(o,str);

  • fac_bsc.instof(o,Instance);

  • fac_bsc.eqany(o,...opts); // opts.includes(o) // this is used to avoid too many === // coz too-many === will make code unreadable, too long

  • fac_bsc.d2kvlist(d) // {a:100,b:200} => [['a','b'],[10,200]]

  • fac_bsc.kvlist2d(kl,vl) // [['a','b'],[10,200]] => {a:100,b:200}

  • fac_bsc.dlngth(d) // {a:100,b:200} => 2

  • fac_bsc.dsome(d,...ks) // dsome({a:100,b:200,c:300},'a','c') => {a:100,c:300}

  • fac_bsc.drekey(d,...ks) // drekey({a:100,b:200,c:300},'A','B','C') => {A:100,B:200,C:300}

  • fac_bsc.dreval(d,...vs) // dreval({a:100,b:200,c:300},10,20,30) => {a:10,b:20,c:30}

  • fac_bsc.SimpleStack //Just Array with a lst(top) setter/getter to handle last-element //and generator from-last-to-first

  • fac_bsc/SimpleMirr

  • fac_bsc.ary_lst_index(ary) {return(ary.length-1)}

  • fac_bsc.ary_lsti(ary) //same as ary_lst_index

  • fac_bsc.get_ary_lst(ary) {return(ary[ary.length-1])}

  • fac_bsc.ary_glst(ary) //same as get_ary_lst

  • fac_bsc.set_ary_lst(ary,v) {ary[ary.length-1] = v}

  • fac_bsc.ary_slst(ary) //same as set_ary_lst

  • fac_bsc.ary_ibfr(ary,index,v) // insert before : ary.splice(index,0,v)

  • fac_bsc.ary_iaft(ary,index,v) // insert after : ary.splice(index+1,0,v)

  • fac_bsc.ary_rm(ary,index) // remove at : ary.splice(index,1)

  • fac_bsc.ary_fltrg(ary,fltr,not=false) //Iterator ,similar as filter, just yield [r,i] // fltr : (r:any,i:number) => Boolean // type Entry = [any,number] //tuple of (r,i) // type IteratorResult = {done: boolean;value:Entry} // type Iterator = { // next(value?: any): IteratorResult;
    // return?(value?: any): IteratorResult; // throw?(e?: any): IteratorResult; // }

  • fac_bsc.ary_freq(ary,v) // ary=[2,2,3,2,4] ary_freq(ary,2) : 3

  • fac_bsc.ary_foreach_brk(arr,f,brkf=(r,i)=>false)

  • fac_bsc.ary_foreach_skip(arr,f,skipf=(r,i)=>false)

CONSTS

    > fac_bsc
    {
      ////the below is just for api-compatible when migrating some code from other coding language  
      
      Nil: [class Nil],
      nfunc: [Function: nfunc],
      None: None,
      nil: [Object: null prototype] {},
      nullptr: [Object: null prototype] {},
      NULL: [Object: null prototype] {},
      
      //// the 4 global symbol to avoid conflicting with undefined , just a semantic-undefined

      empty: Symbol("empty"),
      unknown: Symbol("unknown"),
      impossible: Symbol("impossible"),
      maybe: Symbol("maybe"),
      any:   Symbol("any"),
      hole:  Symbol("hole"),  // for special handling  <empty slot> in Array  

      ////to avoid conflicting with null, just a semantic-null-sign
      root: Symbol("root")

      ///to present noexist, such as when arr=[0,1,2]  arr[4]
      ///currently   js will return undefined  which ambigious with <empty slot>
      ///noexist should be NOT  assignable , 
      /// its IM-possible to implement {assignable:false}  in JS layer
      /// so just use a symbol 
      
      noexist: Symbol("noexist")

      //// for add_repr or add_string_tag

      NUL: '\x00',
      NBSP: ' ',
      LS: '�',
      PS: '�',
      ZWNJ: '‌',
      ZWJ: '‍',
      BOM: '',
    }
    >



    STATE_LABEL_DICT: {
      init: Symbol(init),
      ready: Symbol(ready),
      waiting: Symbol(waiting),
      executing: Symbol(executing),
      pending:   Symbol(pending),
      paused:    Symbol(paused),
      skipped:   Symbol(skipped),
      stopped:   Symbol(stopped),
      finished: Symbol(finished),
      resolved: Symbol(resolved),
      rejected: Symbol(rejected)
      eof: Symbol(eof)
    },

    const DTYPE_NUM = {
        'undefined':                                                0b1n,
        'null':                                                    0b10n,
        'symbol':                                                 0b100n,
        'bool':                                                  0b1000n,
        'int':                                                  0b10000n,
        'float':                                               0b100000n,
        'nan':                                                0b1000000n,
        'infinity':                                          0b10000000n,
        'str':                                              0b100000000n,
        'bigint':                                          0b1000000000n,
        'u8ary':                                          0b10000000000n,
        'u8cary':                                        0b100000000000n,
        'u16ary':                                       0b1000000000000n,
        'u32ary':                                      0b10000000000000n,
        'i8ary':                                      0b100000000000000n,
        'i16ary':                                    0b1000000000000000n,
        'i32ary':                                   0b10000000000000000n,
        'f32ary':                                  0b100000000000000000n,
        'f64ary':                                 0b1000000000000000000n,
        'bu64ary':                               0b10000000000000000000n,
        'bi64ary':                              0b100000000000000000000n,
        'ary':                                 0b1000000000000000000000n,
        'set':                                0b10000000000000000000000n,
        'map':                               0b100000000000000000000000n,
        'wkset':                            0b1000000000000000000000000n,
        'wkmap':                           0b10000000000000000000000000n,
        'wkref':                          0b100000000000000000000000000n,
        'regex':                         0b1000000000000000000000000000n,
        'date':                         0b10000000000000000000000000000n,
        'dv':                          0b100000000000000000000000000000n,
        'arybuf':                     0b1000000000000000000000000000000n,
        'sarybuf':                   0b10000000000000000000000000000000n,
        'promise':                  0b100000000000000000000000000000000n,
        'proxy':                   0b1000000000000000000000000000000000n,
        'sync_generator':         0b10000000000000000000000000000000000n,
        'async_generator':       0b100000000000000000000000000000000000n,
        'normal_dict':          0b1000000000000000000000000000000000000n,
        'cls':                 0b10000000000000000000000000000000000000n,
        'sync_gen':           0b100000000000000000000000000000000000000n,
        'async_gen':         0b1000000000000000000000000000000000000000n,
        'async_func':       0b10000000000000000000000000000000000000000n,
        'normal_func':     0b100000000000000000000000000000000000000000n,
    }

    //// this is used to implement kvtree, which can uniform map/dict/array/set
    ////  which support both nest-at-key-position and nest-at-value-position
    ////  its a super-table structure

    STYPE_DICT: { 
        root: -1, 
        ele: 0,     //element of array
        prop: 1,    //prop of dict
        stele: 2,   //element of set
        mpk: 3,     //key of mp-element
        mpv: 4,     //value of mp-element
        leaf: 5 
    },


    GLOBAL_ERROR_DICT = {
        noexist_is_not_assignable:new Error("noexist_is_not_assignable")
    }

LICENSE

  • ISC