# Utils
There are a couple of functions that are built for common needs. In this section you can find them.
# search()
Searches through your data array with specified options. The search function uses Fuse.js (opens new window) so you can use fuse.js
options to define your search settings.
import { search } from '@pitcher/core'
const data = [
{
title: "Old Man's War",
author: 'John Scalzi',
tags: ['fiction'],
details: {
stock: true,
shop: 'Globus'
}
},
{
title: 'The Lock Artist',
author: 'Steve',
tags: ['thriller'],
details: {
stock: false,
shop: 'Walmart'
}
}
]
const searchFields = ['author', 'details.shop']
// default options - more options here: https://fusejs.io/api/options.html#basic-options
const options = {
threshold: 0.15 // how strict the search should be
useExtendedSearch: true,
distance: 1000
...
...
}
const searchFor = 'John'
const results = search(data, searchFor, searchFields, options)
/* result
[{
title: "Old Man's War",
author: 'John Scalzi',
tags: ['fiction'],
details: {
stock: true,
shop: 'Globus'
}
}]
*/
// If you want to get the default search result of fuse.js
const results = search(data, searchFor, searchFields, options, true)
# uid()
Generates a unique id based on the needs
parameter name | type | default value | |
---|---|---|---|
1 | times | number | 1 |
2 | prefix | string | '' |
import { uid } from '@pitcher/core'
/* default usage */
const uniqueID = uid()
console.log(uniqueID)
// 'e0da844a'
/* longer ID */
const uniqueID = uid(4)
console.log(uniqueID)
// '08536aae-9f22-8e89-2480'
/* with prefix */
const uniqueID = uid(1, 'pid_')
console.log(uniqueID)
// 'pid_4a5303d9'
# link()
Opens an other interactive via a 'pitcher link'
# Example
pitcher://keyword/?myParam=5&otherParam={{ a.count }}
This would open the interactive with the keyword keyword
and save myParam
and otherParam to local storage
import { openLink } from '@pitcher/core'
const link = 'pitcher://keyword/?myParam=5&otherParam={{ a.count}}'
const context = { a: 5 }
openLink(link, context)
This would open a file with fileID: 12345
import { openLink } from '@pitcher/core'
const link = 'pitcher://12345/?myParam=5&otherParam={{ a.count}}'
const context = { a: 5 }
openLink(link, context)
# renderContext()
Renders a template in a given context
import { renderContext } from '@pitcher/core'
const obj = { first: "Hello", last: "World", hasLast: true }
const template = "Say: {{ first }}{% if hasLast %} and then {{ last.toUpperCase() }}{% endif %}"
const result = renderContext(template, obj)
console.log(result)
> // Say: Hello and then World
# contextExec()
WARNING
The following function is considered unsafe. Be sure that you know what you are doing and that the input is validated.
Executes a question template in a give context and returns a Boolean
import { execBool } from '@pitcher/core'
const obj = { a: 5, b: false }
const question = 'a > 5 && !b'
const result = execBool(question, obj)
Executes a question template in a give context and returns a String
import { execString } from '@pitcher/core'
let obj = {
a: () => {
return 'hello world'
},
b: true,
}
let question = "b : a() ? ''"
let result = execString(question, obj) // hello world
obj = { a: 'hello', b: 'world' }
question = "a + ' - ' + b"
result = execString(question, obj) // hello - world
Executes a template in a context
import { execExecute } from '@pitcher/core'
let obj = {
a: () => {
return 'hello world'
},
b: true,
}
let question = 'alert(a())'
let result = execExecute(question, obj) // hello world
# PLATFORM
Returns which platform you currently are on.
If window.iframeMode === true
then CONNECT
value will be skipped.
import { PLATFORM } from '@pitcher/core'
console.log(PLATFORM)
// IOS
Possible values are:
IOS
,CONNECT
,WINDOWS
,ANDROID
andunknown