0.2.0 • Published 4 years ago

@mockpiler/context v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

@mockpiler/context

Context utilities used by MockpilerJS

Helpers

import { helpers } from '@mockpiler/context'
// or import { helpers } from '@mockpiler/compiler'

Array

pick(array[, indexPicker])

Generates a context function to randomly pick an item from the given array. You can optionally pass an indexPicker function to customize the selection.

Usage:

const context = {
  alphabet: helpers.pick(['a', 'b', 'c', 'd', 'e']),
  firstLetter: helpers.pick(['a', 'b', 'c', 'd', 'e'], () => 0)
}

context.alphabet() // Randomly get => 'b'
helpers.firstLetter() // Always get first item => 'a'

tape(array)

Generates a context function to sequentially pick an item from the given array. After reaching the last item, it will start picking again from the beginning.

Usage:

const context = {
  number: helpers.tape(['one', 'two'])
}

context.number() // => 'one'
context.number() // => 'two'
context.number() // => 'one' again

index(array)

Generates a context function to randomly get an index from the given array.

const context = {
  index: helpers.index(['one', 'two', 'three'])
}

context.index() // Randomly => 1

Object

lottery(object[, keys])

Generates a context function to randomly pick a value from the given object. Optionally, you can pass an array of keys to pick from.

const user = {
  name: 'John Doe',
  age: 30
}

const context = {
  userValue: helpers.lottery(user),
  userName: helpers.lottery(user, ['name'])
}

context.userValue() // Randomly => 30
context.userName() // Always get => 'John Doe'

path(keyPath)

Generates a context function to access nested values by using a dot-delimited keyPath.

Note: It also handles accesses to context functions by calling them before accessing their return values.

const context = {
  image: () => ({
    urls: ['https://example.com/image1.jpg', 'https://example.com/image2.jpg'],
    size: {
      width: {
        px: 100
      },
      height: {
        px: 200
      }
    }
  }),
  firstImageUrl: helpers.path('image.urls.0'),
  imageWidth: helpers.path('image.size.width.px')
}

context.firstImageUrl() // => 'https://example.com/image1.jpg'
context.imageWidth() // => 100

Miscellaneous

cacheFirst(fn)

Generates a context function to cache the first result from a given context function.

const randomNumber = helpers.pick([30, 50, 100, 40, 80])

const context = {
  number: helpers.cacheFirst(randomNumber)
}

context.number() // => 30
context.number() // subsequent calls gets the same value => 30