Skip to content

Latest commit

 

History

History
232 lines (135 loc) · 4.23 KB

README.md

File metadata and controls

232 lines (135 loc) · 4.23 KB
permalink
/

package lm

local lm = import "github.com/cakehappens/lonely-mountain/main.libsonnet"

random useful functions

Index

Fields

obj arrays

fn arrays.all

all(fn, arr)

all passes each element of the collection to the given function. all returns true if the function provided never returns false.

PARAMETERS

  • fn (function(x) bool) - where x is a value from the array; and a boolean is returned
  • o (object) - the object to evaluate

RETURNS

bool

fn arrays.any

any(fn, arr)

any passes each element of the collection to the given function. any returns true if the function provided ever returns true.

PARAMETERS

  • fn (function(x) bool) - where x is a value from the array; and a boolean is returned
  • o (object) - the object to evaluate

RETURNS

bool

obj container

fn container.array

array(x)

array takes a single argument of any type. If it is an array, it returns it, otherwise it is wrapped in an array.

fn container.keyValuePairs

keyValuePairs(o)

keyValuePairs takes a single argument of type object. It returns an array, where each element is in the form of {k: v} where k is the field, and v is the value of the field.

obj encoding

fn encoding.manifestJsonOneLine

manifestJsonOneLine(o)

manifestJsonOneLine takes a value of any type, and returns the json representation in a single non-prettified line.

obj obj

fn obj.all

all(fn, o)

all returns true when the function provided returns true for all values in an object

PARAMETERS

  • fn (function(x) bool) - where x is the value of a field in the root object; and a boolean is returned
  • o (object) - the object to evaluate

RETURNS

bool

fn obj.allFields

allFields(fn, o)

allFields returns true when the function provided returns true for all field names in an object

PARAMETERS

  • fn (function(x) bool) - where x is the field name of a field in the root object; and a boolean is returned
  • o (object) - the object to evaluate

RETURNS

bool

fn obj.keysAfterPrune

keysAfterPrune(o)

keysAfterPrune is shorthand for std.objectFields(std.prune(o))

fn obj.lookup

lookup(obj, field, default)

lookup retrieves the value of a single element from an object, given its field. If the given field does not exist, a the given default value is returned instead.

fn obj.lookupAll

lookupAll(obj, field, default)

lookupAll is like lookup but includes hidden fields.

fn obj.values

values(o)

values takes an object and returns an array where each element of the array are the field values in the object.

fn obj.valuesPruned

valuesPruned(o)

valuesPruned is shorthand for std.prune(lm.object.values(o))

obj reflection

fn reflection.arrayOfType

arrayOfType(arr, arr)

arrayOfType takes an array and a type as a string (see std.type(x)). Returns true if all elements in the array of the desired type, otherwise false.

Example usage:

assert assertion.arrayOfType(["a", 1], "string") : "not all items are strings";

obj strings

fn strings.capitalize

capitalize(o)

capitalize takes an string and capitalizes the first letter.