Skip to content

somucheffort/kitsune

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kitsune

kitsune is yet another argument parser, which was introduced in @modularium/fox.

$ npm i @sooomucheffort/kitsune
$ yarn add @sooomucheffort/kitsune

Example

const { KitsuneParser, KitsuneParserError, KitsuneParserType } = require('@sooomucheffort/kitsune')

const kp = new KitsuneParser()

try {
  const parsed = kp.parse(
    [
      'first argument', 
      2,
      false
    ], 
    [
      {
        type: KitsuneParserType.STRING
      }, 
      {
        type: KitsuneParserType.NUMBER
      }, 
      {
        type: KitsuneParserType.BOOLEAN
      }
    ]
  )

  console.log(parsed)
} catch (e) {
  if (e instanceof KitsuneParserError) {
    console.log(e)
    // ...
  } else {
    // ...
  }
}

Types

Standard ones

KitsuneParserType has 3 standard types, which include KitsuneParserType.STRING, KitsuneParserType.NUMBER, KitsuneParserType.BOOLEAN

Creating your own

const { KitsuneParserType } = require('@sooomucheffort/kitsune')

const type = new KitsuneParserType(
  'name', 
  val => /* validate the value */,
  val => /* transform the value */
)

Options for an argument

You can add your options:

// This will throw an error because value isn't a string EXPLICITLY, otherwise it will transform any object to string value
const parsed = kp.parse(
  [
    false
  ], 
  [
    {
      type: KitsuneParserType.STRING,
      explicit: true
    }
  ]
)

count: -1

This option will collect all other arguments in one

const parsed = kp.parse(
  [
    'hi',
    1,
    2,
    3
  ], 
  [
    {
      type: KitsuneParserType.STRING
    },
    {
      type: KitsuneParserType.NUMBER,
      count: -1
    }
  ]
)

console.log(parsed) // [ 'hi', [ 1, 2, 3 ] ]

required: false

With this option, if a type doesn't validate this value it will just ignore it

const oddOrEven = new KitsuneParserType('oddOrEven', 
    (val, opts) => !opts.required || Number.isInteger(val), 
    val => Number.isInteger(val) ? (parseInt(val) % 2) !== 1 ? 'even' : 'odd' : 'neither'
)

/*
const oddOrEven = new KitsuneParserType('oddOrEven', 
    val => Number.isInteger(val), 
    val => (parseInt(val) % 2) !== 1
)
*/

const parsed = kp.parse(
  [
    1,
    'string'
  ], 
  [
    {
      type: oddOrEven
    },
    {
      type: oddOrEven,
      required: false
    }
  ]
)

console.log(parsed) // [ 'odd', 'neither' ]