Skip to content

Latest commit

 

History

History
296 lines (218 loc) · 6.08 KB

Examples.md

File metadata and controls

296 lines (218 loc) · 6.08 KB

type

underscore type --data '[]'
# "array"

underscore type --data '{}'
# "object"

underscore type --data 99
# "number"

underscore type --data '99'
# "number"

underscore type --data '"99"'
# "string"

underscore type --data 'false'
# "boolean"

underscore type --data 'null'
# "null"

underscore type --data 'undefined'
# "undefined"

underscore type --nodata
# "undefined"

print

underscore print --data '[1, 2, 3, 4]'
# [1, 2, 3, 4]

cat example-data/simple.json | underscore print
# { "foo": "bar", "baz": [1, 2, 3] }

echo 'this is a sentence' | underscore print --text
# this is a sentence

run

underscore run 'print("myString")'
# myString

process

underscore process --data '"my-string"' 'camelize(data)'
# "myString"

underscore process --data '[1, 2, 3, 4]' 'max(data)'
# 4

extract

underscore extract field1.field2.field3 --data '{field1: {field2: { field3: 1234 }}}'
# 1234

map

underscore map --data '[1, 2, 3, 4]' 'value+1'
# [2, 3, 4, 5]

underscore map --data '{"a": 1, "b": 2}' 'key === "a" ? value+1 : value-1'
# [2, 1]

underscore map --data '{"a": [1, 4], "b": [2, 8]}' 'value[1]/value[0]'
# [4, 4]

underscore map --data '{"a": [1, 4], "b": [2, 8]}' 'max(value)'
# [4, 8]

reduce

underscore reduce --data '[1, 2, 3, 4]' 'total+value'
# 10

underscore reduce --data '[1, 2, 3, 4]' 'total+""+value'
# "1234"

underscore reduce --data '[1, 2, 3, 4]' 'total+""+value' 0
# "01234"

reduceRight

underscore reduceRight --data '[1, 2, 3, 4]' 'total+value'
# 10

underscore reduceRight --data '[1, 2, 3, 4]' 'total+""+value'
# "4321"

underscore reduceRight --data '[1, 2, 3, 4]' 'total+""+value' 0
# "04321"

underscore -i example-data/earthporn.json select '.children .title'
# [
#   "Eating breakfast in the Norwegian woods! Captured with my phone [2448x3264] ",
#   "The Rugged Beauty of Zion NP Utah at Sunrise [OC] (1924x2579)",
#   "Falls and island near Valdez, AK on a rainy day [4200 x 3000]",
#   "Havasu Falls, Havasupai, Arizona.  This is a personal photograph from a trip I took in 2006 before the flood of 2008 [960x1280]",
#   "Victoria Amazonica in Brazil, SC [2448X3264]",
#   "A dreamy haze flows over the Alaska Range on the Castner Glacier [4948 x 3280]"
# ]

find

underscore find --data '[1, 2, 3, 4]' 'value > 2'
# 3

underscore find --data '{"foo": 1, "bar": 2}' 'key == "bar"'
# 2

filter

underscore filter --data '[1, 2, 3, 4]' 'value > 2'
# [3, 4]

underscore filter --data '{"foo": 1, "bar": 2}' 'key == "bar"'
# [2]

reject

underscore reject --data '[1, 2, 3, 4]' 'value > 2'
# [1, 2]

underscore reject --data '{"foo": 1, "bar": 2}' 'key == "bar"'
# [1]

flatten

underscore flatten --data '[1, [2, [3]], 4]'
# [1, 2, 3, 4]

underscore flatten --shallow --data '[1, [2, [3]], 4]'
# [1, 2, [3], 4]

pluck

underscore pluck --data "[{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}]" name
# ["moe", "larry", "curly"]

keys

underscore keys --data '{name : "larry", age : 50}'
# ["name", "age"]

underscore keys --data '[8, 9, 10]'
# ["0", "1", "2"]

values

underscore values --data '{name : "larry", age : 50}'
# ["larry", 50]

underscore values --data '[8, 9, 10]'
# [8, 9, 10]

extend

underscore extend --data '{name : "larry", age : 50}' '{age: 65}'
# { "name": "larry", "age": 65 }

defaults

underscore defaults --data '{name : "larry", age : 50}' '{name: "unknown", salary: 100}'
# { "name": "larry", "age": 50, "salary": 100 }

any

underscore any --data '[1, 2, 3, 4]' 'value > 5'
# false

underscore any --data '[1, 2, 3, 4]' 'value > 2'
# true

underscore any --data '[1, 2, 3, 4]' 'value > 0'
# true

underscore any --data '{foo: 1, bar: 2}' 'key == "bar"'
# true

all

underscore all --data '[1, 2, 3, 4]' 'value > 5'
# false

underscore all --data '[1, 2, 3, 4]' 'value > 2'
# false

underscore all --data '[1, 2, 3, 4]' 'value > 0'
# true

underscore all --data '{"foo": 1, "bar": 2}' 'key == "bar"'
# false

isObject

underscore isObject --data '{name : "larry", age : 50}'
# true

underscore isObject --data '[]'
# false

underscore isObject --data '[]' --arrays-are-objects
# true

isArray

underscore isArray --data '{name : "larry", age : 50}'
# false

underscore isArray --data '[]'
# true

isString

underscore isString --data '{}'
# false

underscore isString --data '[]'
# false

underscore isString --data "'foo'"
# true

isNumber

underscore isNumber --data '{}'
# false

underscore isNumber --data '[]'
# false

underscore isNumber --data "'9'"
# false

underscore isNumber --data "9"
# true

isBoolean

underscore isBoolean --data '0'
# false

underscore isBoolean --data '1'
# false

underscore isBoolean --data 'true'
# true

underscore isBoolean --data 'false'
# true

isNull

underscore isNull --data '0'
# false

underscore isNull --data '1'
# false

underscore isNull --data 'false'
# false

underscore isNull --data 'null'
# true

underscore isNull --data 'undefined'
# false

isUndefined

underscore isUndefined --data '0'
# false

underscore isUndefined --data '1'
# false

underscore isUndefined --data 'false'
# false

underscore isUndefined --data 'null'
# false

underscore isUndefined --data 'undefined'
# true

template

underscore template --data '{name: "moe"}' example-data/trivial.template
# hello moe

underscore template --nodata example-data/trivial.template
# module.exports = function(obj){var __p='', print=function(){__p+=Array.prototype.join.call(arguments, '')};with(obj||{}){__p+='hello '+( name )+'\n'+
# '';} return __p; }