1.22.9 • Published 24 hours ago

@fishertsau/moonlight v1.22.9

Weekly downloads
1
License
ISC
Repository
github
Last release
24 hours ago

moonlight

  • 提供JavaScript常用的功能,類似helper或是utility,讓javascript專案使用
  • 可用於Server-side專案,如node.js,或是Client-side專案,如vue,react中
  • 發佈於npm中,使用時當作一個package使用
  • 可用於es6與commonjs中

Main functions

  1. Validator
  2. Parser
  3. time
  4. array
  5. hostname
  6. Checker
  7. Utils

Input Validator

  • check the input validity with specified rules
  • only the validated attribute(s) are returned
  • convert data type on validating, e.g '100' to 100 for 'type:number'
  • rules

  • validator:: obj -> obj -> obj

  • examples

       const rules = {
            foo: 'required|type:number',
            bar: 'required|same:foo',
            koo: 'type:datetime',
            loo: 'validValues:[v1,v2,v3]',
            moo: 'required|type:nonEmptyString',
            aoo: 'type:array',
            soo: 'type:[number,null,nonEmptyString]',
       };
    
      const validatedData = { foo: 123, bar: 123, koo: '2000-01-01', loo: 'invalidValue', moo: '', aoo:'notArray'};
      let result = validator(rules)(validatedData);
      // => { validated: true ,  values:{foo:123, bar:123,koo:'2000-01-01'}}
      // => { validated: false ,
      //      errors: {
      //         foo: ['foo should be a number.'], 
      //         koo: ['koo should be a string.']
      //         loo: ['loo should be in one of the values: v1,v2,v3.']
      //         moo: ['moo should be an non empty string.']
      //         aoo: ['aoo should be in array format.']
      //      }
      //    }

array

  • shuffle :: array -> array
    • re-order the array items randomly

hostname

  • isValidHostname :: String -> Boolean
    • To validate a give hostname

Checker

  • isTrue :: a -> boolean

    • To check if a given value is true
  • isEmpty :: a -> boolean

    • To check is a given value is empty
  • isIntBetween:: int a -> int b -> int c -> boolean

    • To check is a given int or value is between the specified range
  • isNumber:: a -> boolean

    • To check is given value is a number
  • isString:: a -> boolean

    • To check is given value is a string
  • isObject:: a -> boolean

    • To check is given value is an object
    • null and array are excluded
  • isValidEmail:: a -> boolean

    • To check is given value is a valid email
  • isValidBool:: a -> boolean

    • To check is given value is a valid boolean value
  • isFunction:: a -> boolean

    • To check is given value a Function
  • isValidHostname:: a -> boolean

    • To check if a given value a valid hostname
  • isDate:: str a -> boolean

    • To check is given string is in date format
    • valid date format: yyyy-mm-dd (time is not included)
  • isDateTime:: str a -> boolean

    • To check is given string is in datetime format
    • valid datetime format includes: unix time (integer), date+time, UTC
  • isIsoFormat:: a -> boolean

    • To check is given value is in ISO 8601 format
  • isIsoFormatWithZeroOffset:: a -> boolean

    • To check is given value is in ISO 8601 format with zero offset

MoonUse

  • useRetry

  • useLock

  • useWait

    • To make an action execution wait for a given time before execution
      useWait(1000, action);
      //=> wait for 1000ms, and then run action 
    - ```
  • useRedisCache

    • To get value from redis cache, otherwise get the value and cache it
      const foo = await useCache('someCacheKey', getter, {keyLife:100});
      //=> keyLife: key有效時間 (in second)
    - ```

time

  • now :: null -> DateTime

        now();
        //=> current time
  • isoStrToUtcStr:: isoDatetimeString -> utcDatetimeString

        isoStrToUtcStr('1997-07-16T19:20:35+03:00');
        //=> '1997-07-16 16:20:35'
  • isoStrToTaipeiStr :: isoDatetimeString -> taipeiDatetimeString

       isoStrToTaipeiStr('1997-07-16T19:20:35+03:00');  // utc: 1997-07-16T16:20:35
       //=> '1997-07-17 00:20:35'

Utils

  • clearSpace:: string -> string

    • To clear or remove space in a string
  • trim

    • To trim the string(s) in object properties and values
    • To trim string(s) in list
    • Can trim string(s) in nested objects or nested array
       trim({
          'p1  ': 'foo ',
          p2: ['  abc', ' def  '],
          p3: { p3_1: ['p31 '] },
          p4: { p4_1: ['p41 ', ' p42'], 'p4_2': {} },
          p5: 100,
         });
       //=> 
        {
          p1: 'foo',
          p2: ['abc', 'def'],
          p3: { p3_1: ['p31'] },
          p4: { p4_1: ['p41', 'p42'], 'p4_2' : {} },
          p5: 100,
        }
  • extractByPath

    • To extract a value from a structured collection object

    • Object structure

       {
          k1: {
            k1a: {}
            k1b: {}
          },
          k2: {
            k2a: {}
            k2b: {}
          }
       }
    • example:
      extractByPath(['info', 'age'])({'person1':{info:{age:10}}, 'person2':{info:{age:20}}});
      //=>  {'person1':10, 'person2':20}
  • getDirty

    • To get the values in new object which differ from that in the original object
    • The function is auto-curry

    • example:

     oriObj = {a:1, b:3}
     newObj = {a:1, b:5, c:7}
    
     getDirty(oriObj)(newObj);
      //=> {b:5, c:7}
  • renameKey

    • To change a key name

    • example:

     oriObj = {foo:1}
    
     renameKey('foo','bar')(oriObj);
      //=> {bar:1}
  • createEventEmitter

    • To create an event emitter

    • example:

    const em = createEventEmitter(); 
    
    // register event handler
    em.on('someEvent', someHandler); 
    
    // remove event handler 
    // A. remove specific handler
    em.remove('someEvent', someHandler); 
    // B. remove all handlers
    em.remove('someEvent'); 
    
    // send out event
    em.emit('someEvent', payload); 
  • pluckObjProp

    • To pluck a nested obj prop

    • example:

      // Functor f => k -> {k:v} -> {k:v}
      const obj = {
                    prop1: { foo: 123, bar: 'abc' },
                    prop2: { foo: 999, bar: 'abc' }
            }
    
      pluckObjProp('foo')(obj)
    
      //=> {prop1: 123, prop2: 999}
  • clean

    • To remove obj props if value is undefined/null/emptyString
    • Applicable to nested object
       clean({
          foo: 123,
          bar: undefined,
          koo: {
            k1: 'abc',
            k2: undefined,
          },
          poo: null,
          moo: '',
      })
       //=> { foo: 123, koo: { k1:'abc'} }
  • cleanNilUndefined

    • To remove obj props if value is undefined/null
    • Applicable to nested object
       clean({
          foo: 123,
          bar: undefined,
          koo: {
            k1: 'abc',
            k2: undefined,
          },
          poo: null,
          moo: '',
      })
       //=> { foo: 123, koo: { k1:'abc'} moo: '' }
  • hasValue

    • To check if an object has specified value
    • Applicable to simple object
       hasValue(123, { foo: 123 })
       //=> true
    
       hasValue('abc', { foo: 123 })
       //=> false 
  • rmArrSquareInReqParamsKey

       harmArrSquareInReqParamsKeys(
          {
            from: 0,
            'ids[]': [ 'abc1705548556993', 'abc1705558802725']
          });
    
       //=> 
          {
             from: 0,
             'ids' : [ 'abc1705548556993' , 'abc1705558802725']
           };
  • getObjectDifference

       const obj1 = {
          a: 1,
          b: {
            c: 2,
            d: {
              e: 3,
            },
          },
          f: {
            g: 5,
          },
        };
    
      const obj2 = {
          a: 1,
          b: {
            c: 3,
            d: {
              e: 4,
            },
          },
        };
    
       getObjectDifference(obj1, obj2);
    
       //=> 
          { { 'b.c': [2, 3], 'b.d.e': [3, 4], f: [{ g: 5 }, undefined] }

Parser

  • parseObj :: obj -> QueryString

    • convert an object to query string
    • example:
        parsrStr({a:'foo',b:'bar'})
        //=> a=foo&b=bar
  • parseStr :: QueryString -> obj

    • convert a query string to an object
    • example:
        parsrObj('a=foo&b=bar')
        //=> {a:'foo',b:'bar'}
  • convertKeyValue :: a -> obj (a: 'str=str')

    • convert a key-value array to an object
       convertKeyValue(['foo=bar', 'fiz=fuz']);
       //=> {foo:'bar', fiz:'fuz'}
  • convertObjToArr :: obj -> a

    • convert an object to a key-value array
       convertKeyValue({ foo: 'bar', fiz: 'fuz' });
       //=> ['foo=bar', 'fiz=fuz']
  • snakeToCamel :: str -> str

    • convert a snake-string to camel-case string
       snakeToCamel('abc_def_ghi')
       //=> 'abcDefGhi'
  • objKeyToCamel :: obj -> obj

    • convert all keys in an object from snake to camel string
    • nested object is supported
       objKeyToCamel({this_is_key: val})
       //=> {thisIsKey: val}
  • camelToSnake :: str -> str

    • convert a camel-case string to snake-case string
       camelToSnake('abcDefGhi')
       //=> 'abc_def_ghi'
  • objKeyToSnake :: obj -> obj

    • convert all keys in an object from camel to snake-case string
    • nested object is supported
       objKeyToSnake({ thisIsKey: 'someVal' })
       //=> { this_is_key: 'someVal' }
  • parseCookie :: string -> obj

    • parse a cookie string to an object
       parseCookie('foo=bar ; equation=E%3Dmc%5E2;   asd=');
       //=> { foo: 'bar', equation: 'E=mc^2', asd: '' }
  • serializeCookie :: obj -> string

    • serialize an object to cookie string
       serializeCookie({ foo: 'bar', equation: 'E=mc^2', asd: '' });
       //=> 'foo=bar;equation=E%3Dmc%5E2;asd='
1.22.7

1 day ago

1.22.8

24 hours ago

1.22.9

24 hours ago

1.22.4

3 days ago

1.22.5

3 days ago

1.22.6

3 days ago

1.22.3

6 days ago

1.22.2

6 days ago

1.22.1

7 days ago

1.21.1

8 days ago

1.21.4

8 days ago

1.21.2

8 days ago

1.21.3

8 days ago

1.20.1

9 days ago

1.20.2

9 days ago

1.19.12

13 days ago

1.19.11

16 days ago

1.19.10

17 days ago

1.19.8

19 days ago

1.19.7

19 days ago

1.19.9

19 days ago

1.19.6

21 days ago

1.19.5

21 days ago

1.19.4

23 days ago

1.19.3

25 days ago

1.19.2

25 days ago

1.19.1

25 days ago

1.18.14

1 month ago

1.18.13

1 month ago

1.18.9

1 month ago

1.18.12

1 month ago

1.18.11

1 month ago

1.18.10

1 month ago

1.18.8

1 month ago

1.18.7

1 month ago

1.18.5

1 month ago

1.18.4

1 month ago

1.18.6

1 month ago

1.18.3

1 month ago

1.18.1

1 month ago

1.18.2

1 month ago

1.17.2

1 month ago

1.17.4

1 month ago

1.17.3

1 month ago

1.16.18

2 months ago

1.16.17

2 months ago

1.17.1

2 months ago

1.16.14

2 months ago

1.16.13

2 months ago

1.16.12

2 months ago

1.16.16

2 months ago

1.16.15

2 months ago

1.16.10

2 months ago

1.16.11

2 months ago

1.16.9

2 months ago

1.16.8

2 months ago

1.16.7

2 months ago

1.15.6

2 months ago

1.15.5

2 months ago

1.15.4

2 months ago

1.15.3

2 months ago

1.15.2

2 months ago

1.15.1

2 months ago

1.14.4

2 months ago

1.14.3

2 months ago

1.14.2

2 months ago

1.14.1

2 months ago

1.13.24

3 months ago

1.13.23

3 months ago

1.13.22

3 months ago

1.13.21

3 months ago

1.13.20

3 months ago

1.13.19

3 months ago

1.13.18

3 months ago

1.13.17

4 months ago

1.13.16

4 months ago

1.13.15

4 months ago

1.13.14

4 months ago

1.13.13

4 months ago

1.13.6

4 months ago

1.13.9

4 months ago

1.13.11

4 months ago

1.13.8

4 months ago

1.13.10

4 months ago

1.13.7

4 months ago

1.13.12

4 months ago

1.13.5

4 months ago

1.13.4

4 months ago

1.13.3

5 months ago

1.12.16

5 months ago

1.12.15

5 months ago

1.12.18

5 months ago

1.12.17

5 months ago

1.13.2

5 months ago

1.13.1

5 months ago

1.12.14

5 months ago

1.10.2

7 months ago

1.9.10

8 months ago

1.11.2

7 months ago

1.11.1

7 months ago

1.12.3

6 months ago

1.12.2

6 months ago

1.12.1

6 months ago

1.12.7

6 months ago

1.12.6

6 months ago

1.12.5

6 months ago

1.12.4

6 months ago

1.8.2

10 months ago

1.8.1

10 months ago

1.12.9

6 months ago

1.12.8

6 months ago

1.12.10

6 months ago

1.9.9

10 months ago

1.9.8

10 months ago

1.9.7

10 months ago

1.9.6

10 months ago

1.12.12

6 months ago

1.9.5

10 months ago

1.12.11

6 months ago

1.9.4

10 months ago

1.9.3

10 months ago

1.12.13

6 months ago

1.7.1

11 months ago

1.10.10

7 months ago

1.8.3

10 months ago

1.6.13

11 months ago

1.6.15

11 months ago

1.6.14

11 months ago

1.6.17

11 months ago

1.6.16

11 months ago

1.6.19

11 months ago

1.6.18

11 months ago

1.6.12

11 months ago

1.6.9

11 months ago

1.6.8

11 months ago

1.6.7

11 months ago

1.6.6

11 months ago

1.6.11

11 months ago

1.6.10

11 months ago

1.6.5

11 months ago

1.6.4

11 months ago

1.6.3

11 months ago

1.6.2

12 months ago

1.5.5

1 year ago

1.5.4

1 year ago

1.5.3

1 year ago

1.5.2

1 year ago

1.5.1

1 year ago

1.5.0

1 year ago

1.4.6

1 year ago

1.4.5

1 year ago

1.5.9

1 year ago

1.5.8

1 year ago

1.5.7

1 year ago

1.5.6

1 year ago

1.5.10

1 year ago

1.5.12

1 year ago

1.5.11

1 year ago

1.5.14

1 year ago

1.5.13

1 year ago

1.5.16

1 year ago

1.5.15

1 year ago

1.5.18

12 months ago

1.5.17

12 months ago

1.5.19

12 months ago

1.5.21

12 months ago

1.5.20

12 months ago

1.4.9

1 year ago

1.5.23

12 months ago

1.4.8

1 year ago

1.5.22

12 months ago

1.4.7

1 year ago

1.4.4

1 year ago

1.4.3

1 year ago

1.4.2

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.4

1 year ago

1.3.3

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.12

1 year ago

1.2.13

1 year ago

1.2.10

1 year ago

1.2.11

1 year ago

1.2.14

1 year ago

1.2.15

1 year ago

1.2.9

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.1.19

2 years ago

1.1.18

2 years ago

1.1.17

2 years ago

1.1.16

2 years ago

1.1.15

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.14

2 years ago

1.1.13

2 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.0.44

3 years ago

1.0.43

3 years ago

1.0.47

3 years ago

1.0.46

3 years ago

1.0.45

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.39

3 years ago

1.0.17

3 years ago

1.0.38

3 years ago

1.0.16

3 years ago

1.0.40

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.42

3 years ago

1.0.20

3 years ago

1.0.41

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.33

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.37

3 years ago

1.0.15

3 years ago

1.0.36

3 years ago

1.0.35

3 years ago

1.0.34

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.9

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago