tml-core

TML extended basic types

npm install tml-core
8 downloads in the last week
16 downloads in the last month

tml-core(3) -- extended basic types and methods

SYNOPSIS

tml-core provides basic type extensions and methods.

OPTIONS

This is a test.

SYNTAX

ENVIRONMENT

RETURN VALUES

STANDARDS

SECURITY CONSIDERATIONS

BUGS

HISTORY

AUTHOR

SEE ALSO

IMPLEMENTATION


Core

@purpose General purpose types and methods;
@author  Erin Phillips;
@version 0.0.2-3;
@history 2013-03-25 emp Initial version;
    @history 2013-04-30 EMP [0.0.2-1] add-tml-build;
    @history 2013-04-30 EMP [0.0.2-2] fix-description;
    @history 2013-04-30 EMP [0.0.2-3] fix-description;

resources

TMLR = tml-runtime [TmlRuntime]
    @purpose Core TMLR runtime;

MOMENT = moment [moment]

locals

json = TMLR::json

code

-- STATICS --

Shared date routines;

anvil getLongDate ->$

    =MOMENT().format('LLL');
    @purpose returns long formatted date string;

anvil getTimestamp ->$

    =MOMENT().format('YYYYMMDDHHmmss');
    @purpose returns fixed-width timestamp;

anvil getDate ->$

    =MOMENT().format('YYYY-MM-DD');
    @purpose returns yyyy-mm-dd;

-- ASPECTS --

Cross-cutting concerns;

aspect Hash(@hash%={})

method deleteKey $ -> !

    =exists @hash a
    delete @hash.(a);

method getValue $ -> ?

    =@hash.(a);

method getHash -> %

    =@hash;

method setValue $ ? -> this

    @hash.(a)=b;

method hasKey $ -> !

    =exists @hash a;

-- PARAMETERIZED TYPES --;

pattern patRefType :name :typ :default

<<<

type Ref:name(@v:typ=:default)

method get ->:typ

    =@v;

method set :typ->this

    @v=a;
>>>

render patRefType Int # 0;

render patRefType String $ '';


pattern patLoadMatch :name :typ :src :proc

    <<<

method loadMatch:name src:typ tgt%RefString grp#=0 -> ret!

    =false
    vars l=scan :src @re;
    if l
        ret=true
        tgt.v=l[grp]
        :proc;;
    >>>

pattern patLoadTuple :name :typ :src :proc

    <<<

method loadTuple:name src:typ tgt* -> ret!

    =false
    vars l=scan :src @re;
    if l
        ret=true
        each l as item
            push tgt item;;
        :proc;;
    >>>

pattern patLoadMap :name :typ :src :proc

    <<<

method loadMap:name src:typ tgt% igrp# vgrp#=0 -> ret!

    =false
    vars l=scan :src @re;
    if l
        ret=true
        tgt.(l[igrp])=l[vgrp]
        :proc;;
    >>>

pattern patLoadAll :name

    <<<

render patLoad:name $ src ;

render patLoad:name Excise %RefString src.v

    <<<excise= src.v l.index length l[0]>>>;

render patLoad:name Advance %RefString src.v

    <<<excise= src.v 0 (add l.index length l[0])>>>;
    >>>

type Regex(@pat$)

    @re=newex RegExp(@pat,'m');

method asString -> $

    =@re.toString();

method setModifiers $ -> this

    @re=newex RegExp(@pat,a);

render patLoadAll Match;

render patLoadAll Tuple;

render patLoadAll Map;

method markMatch src$ tgt%RefString span#=20 opt sav%RefString-> ret!

    =false
    vars l=scan src @re
         lhs rhs;
    if l
        ret=true
        if sav sav.v = l[0];
        lhs = sub l.index span;
        if lt lhs 0 lhs=0;
        rhs = add l.index length l[0];
        tgt.v=
            con
                extract src lhs (sub l.index lhs)
                ' >>> '
                l[0]
                ' <<< '
                extract src rhs span;
        ;;

-- TESTS --;

test REGEX

    vars re=new Regex('[a-z]+(\\d+)')
         reg=new Regex('([a-z]+)(\\d+)([a-z]+)');

test NO_POSTPROC

    vars src='aaa999bbb';

test LOAD

    vars v=new RefString();
    re.loadMatch(src,v)

test-eq : should load a match

    v.v vs 'aaa999'
    ;

test MARKMATCH

    vars v=new RefString();
    re.markMatch(src,v)

test-eq : should load a match

    v.v vs ' >>> aaa999 <<< bbb'
    ;

test LOAD_GROUP

    vars v=new RefString();
    re.loadMatch(src,v,1)

test-eq : should load a match group

    v.v vs '999'
    ;

test LOAD_TUPLE

    vars v=[];
    reg.loadTuple(src,v)

test-eq : should load overall match as first tuple value

    v[0] vs src

test-eq : should load first group as item[1]

    v[1] vs 'aaa'

test-eq : should load second group as item[2]

    v[2] vs '999'

test-eq : should load third group as item[3]

    v[3] vs 'bbb'
    ;

test LOAD_MAP

    vars v={};
    reg.loadMap(src,v,1)

test-eq : should set overall match to index key in hash

    v.aaa vs src
    ;

    ;

test POSTPROC

test LOAD_EXCISE

    vars v=new RefString()
         src=new RefString('aaa999bbb');

    re.loadMatchExcise(src,v)

test-eq : should load a match

    v.v vs 'aaa999'

test-eq : should remove match from source

    src.v vs 'bbb'

    ;

test LOAD_EXCISE_GROUP

    vars v=new RefString()
         src=new RefString('aaa999bbb');

    re.loadMatchExcise(src,v,1)

test-eq : should load a match

    v.v vs '999'

test-eq : should remove match from source

    src.v vs 'bbb'

    ;

test LOAD_ADVANCE

    vars v=new RefString()
         src=new RefString('bunch of stuff\n\naaa999bbbx');

    re.loadMatchAdvance(src,v)

test-eq : should load a match

    v.v vs 'aaa999'

test-eq : should remove match from source

    src.v vs 'bbbx'

    ;

test LOAD_ADVANCE_GROUP

    vars v=new RefString()
         src=new RefString('bunch of stuff\n\naaa999bbbx');

    re.loadMatchAdvance(src,v,1)

test-eq : should load a match

    v.v vs '999'

test-eq : should advance beyond the match

    src.v vs 'bbbx'

    ;

test LOAD_TUPLE_EXCISE

    vars v=[]
         src=new RefString('000aaa999bbbx111');
    reg.loadTupleExcise(src,v)

test-eq : should load overall match as first tuple value

    v[0] vs 'aaa999bbbx'

test-eq : should load first group as item[1]

    v[1] vs 'aaa'

test-eq : should load second group as item[2]

    v[2] vs '999'

test-eq : should load third group as item[3]

    v[3] vs 'bbbx'

test-eq : should remove matched text from string

    src.v vs '000111'
    ;        

test LOAD_TUPLE_ADVANCE

    vars v=[]
         src=new RefString('000aaa999bbbx111');
    reg.loadTupleAdvance(src,v)

test-eq : should load overall match as first tuple value

    v[0] vs 'aaa999bbbx'

test-eq : should load first group as item[1]

    v[1] vs 'aaa'

test-eq : should load second group as item[2]

    v[2] vs '999'

test-eq : should load third group as item[3]

    v[3] vs 'bbbx'

test-eq : should advance beyond the matched text

    src.v vs '111'
    ;        

test LOAD_MAP_EXCISE

    vars v={}
         src=new RefString('000aaa999bbbx111');
    reg.loadMapExcise(src,v,1)

test-eq : should set overall match to index key in hash

    v.aaa vs 'aaa999bbbx'

test-eq : should remove matched text from string

    src.v vs '000111'
    ;        

test LOAD_MAP_ADVANCE

    vars v={}
         src=new RefString('000aaa999bbbx111');        
    reg.loadMapAdvance(src,v,1)

test-eq : should set overall match to index key in hash

    v.aaa vs 'aaa999bbbx'

test-eq : should remove matched text from string

    src.v vs '111'
    ;        

    ;

    ;

test DATE_FORMAT

    vars o1=getLongDate()
         o2=getTimestamp()
         o3=getDate();

test-true : should return formatted date

    match o1 /^\w+\s\d+\s\d+\s\d+[:]\d+\s[aApP][mM]$/

test-true : should return formatted timestamp

    match o2 /^\d{14}$/

test-true : should return formatted yyyy-mm-dd

    match o3 /^\d{4}[-]\d{2}[-]\d{2}$/

    ;

test HASH

test-eq : should set value

    (new Hash()).setValue('a',99).getValue('a') vs 99

test-true : should delete value

    (new Hash()).setValue('a',99).deleteKey('a')

test-false : should detect failed delete

    (new Hash()).setValue('b',99).deleteKey('a')

test-true : should detect hash value

    (new Hash()).setValue('a',99).hasKey('a')

test-false : should detect missing hash value

    (new Hash()).setValue('a',99).hasKey('b')

    ;

test REFINT

test-eq : should return an integer value

    (new RefInt(5)).get() vs 5

test-eq : should get the default value zero

    (new RefInt()).get() vs 0

    ;

test REFSTRING

test-eq : should return a string value

    (new RefString('aa')).get() vs 'aa'

test-eq : should get the default value

    (new RefString()).get() vs ''

    ;
npm loves you