Helpers
Formidable includes a variety of "helper" functions. Many of these functions are used by the framework itself; however, you are free to use them in your own applications if you find them convenient.
Usage
To use a formidable helper function, you need to import it from @formidablejs/framework/lib/Support/Helpers:
- Imba
- TypeScript
import { slug } from '@formidablejs/framework/lib/Support/Helpers'
slug('Donald Pakkies') # donald-pakkies
import { slug } from '@formidablejs/framework/lib/Support/Helpers'
slug('Donald Pakkies') // donald-pakkies
Available Functions
Arrays & Objects
asObject
The asObject function, converts a custom object into a JavaScript object:
- Imba
- TypeScript
const object = asObject(customObject)
const object = asObject(customObject)
dotNotation
The dotNotation function turns an object into a single level value that uses "dot" notation to indicate depth:
- Imba
- TypeScript
const object = {
app: {
name: 'Formidable'
}
}
const appName = dotNotation(object, 'app.name')
const object = {
app: {
name: 'Formidable'
}
}
const appName = dotNotation(object, 'app.name')
You may also use dot, an alias of dotNotation:
- Imba
- TypeScript
const object = {
app: {
name: 'Formidable'
}
}
const appName = dot(object, 'app.name')
const object = {
app: {
name: 'Formidable'
}
}
const appName = dot(object, 'app.name')
without
The without helper removes specified data from the given object:
- Imba
- TypeScript
without({
name: 'Donald',
city: 'East Rand'
}, ['city'])
# { name: 'Donald' }
without({
name: 'Donald',
city: 'East Rand'
}, ['city'])
// { name: 'Donald' }
isArray
The isArray helper checks if the given variable is a valid Array:
- Imba
- TypeScript
if isArray([])
# do something
if (isArray([])) {
// do something
}
isBoolean
The isBoolean helper checks if the given variable is a valid Boolean:
- Imba
- TypeScript
if isBoolean(variable)
# do something
if (isBoolean(variable)) {
// do something
}
isClass
The isClass helper checks if the given variable is a valid Class:
- Imba
- TypeScript
if isClass(variable)
# do something
if (isClass(variable)) {
// do something
}
isFunction
The isFunction helper checks if the given variable is a valid Function:
- Imba
- TypeScript
if isFunction(variable)
# do something
if (isFunction(variable)) {
// do something
}
isNumber
The isNumber helper checks if the given variable is a valid Number:
- Imba
- TypeScript
if isNumber(variable)
# do something
if (isNumber(variable)) {
// do something
}
isObject
The isObject helper checks if the given variable is a valid Object:
- Imba
- TypeScript
if isObject(variable)
# do something
if (isObject(variable)) {
// do something
}
isString
The isString helper checks if the given variable is a valid String:
- Imba
- TypeScript
if isString(variable)
# do something
if (isString(variable)) {
// do something
}
toBoolean
The toBoolean helper converts the given variable into a Boolean value:
- Imba
- TypeScript
toBoolean('true') # true
toBoolean(true) # true
toBoolean(1) # true
toBoolean('true') // true
toBoolean(true) // true
toBoolean(1) // true
wildcard
The wildcard helper checks if the given variable matches a wildcard:
- Imba
- TypeScript
wildcard('/user/*/edit', '/user/1/edit') # true
wildcard('/tasks/*', 'tasks/learn-imba') # true
wildcard('/user/*/edit', '/user/1/edit') // true
wildcard('/tasks/*', 'tasks/learn-imba') // true
Strings
slug
The slug helper converts the given string into a slug:
- Imba
- TypeScript
slug('Hello world', '-') # hello-world
slug('Hello world', '-') # hello-world
strRandom
The strRandom helper generates a random string:
- Imba
- TypeScript
strRandom() # bfd809fc
# with custom length
strRandom(40) # 485f8c73737030df7872e2c3e5e2d3b0eb1d769f
strRandom() // bfd809fc
# with custom length
strRandom(40) // 485f8c73737030df7872e2c3e5e2d3b0eb1d769f
length must be divisible by 2.
Security
encrypt
The encrypt helper encrypts an object.
- Imba
- TypeScript
encrypt('Hello World') # f8867ec8f7960de147f4c2da37fe4b99
encrypt('Hello World') // f8867ec8f7960de147f4c2da37fe4b99
decrypt
The decrypt helper decrypts an encrypted value.
- Imba
- TypeScript
decrypt('f8867ec8f7960de147f4c2da37fe4b99') # Hello World
decrypt('f8867ec8f7960de147f4c2da37fe4b99') // Hello World
Miscellaneous
now
The now helper returns the current timestamp instance for Database queries:
- Imba
- TypeScript
now!
now()
expiresIn
The expiresIn helper creates an expiration date for Redis:
- Imba
- TypeScript
expiresIn('2 minutes')
expiresIn('1 hour')
expiresIn('3 days')
expiresIn('2 minutes')
expiresIn('1 hour')
expiresIn('3 days')
config
The config function gets the value of a configuration variable. The configuration values are accessed using "dot" syntax, which includes the name of the file and the option you wish to access. A default value may be specified and is returned if the configuration option does not exist:
- Imba
- TypeScript
const appName = config('app.name')
const appName = config('app.name', 'Something else')
const appName = config('app.name')
const appName = config('app.name', 'Something else')
env
The env function retrieves the value of an environment variable or returns a default value:
- Imba
- TypeScript
const appUrl = env('APP_URL')
const appUrl = env('APP_URL', 'http://127.0.0.1:3000')
const appUrl = env('APP_URL')
const appUrl = env('APP_URL', 'http://127.0.0.1:3000')
response
The response function returns a response object to the client:
- Imba
- TypeScript
response('Hello', 200)
response().json({
message: 'Hello'
})
response().code(200)
response('Hello', 200)
response().json({
message: 'Hello'
})
response().code(200)
view
The view function returns a view response to the client:
- Imba
- TypeScript
view(Profile, { user }, 200)
view(Profile, { user }, 200)
tap & multitap
The tap function accepts two arguments: an arbitrary value and a callback. The value will be passed to the callback and then be returned by the tap function. The return value of the callback is irrelevant:
- Imba
- TypeScript
const user = tap User.forge!.first!, do(user)
user.name = 'Donald'
user.save!
const user = tap(User.forge().first(), (user: object) => {
user.name = 'Donald'
user.save()
})
If no callback is passed to the tap function, you may call any method on the given value. The return value of the method you call will always be value, regardless of what the method actually returns in its definition:
- Imba
- TypeScript
user = tap(user).save({
name: 'Donald'
})
const user = tap(user).save({
name: 'Donald'
})
You may also use the multitap function, this function allows you to access any method that's part of value by always returning value after you call a value method:
- Imba
- TypeScript
const user = multitap(user)
.setName('Donald')
.setLocation('East Rand')
.untap!
const user = multitap(user)
.setName('Donald')
.setLocation('East Rand')
.untap()
In the example above, we have a untap method which we call at the end of our method calls, we do this so we can exit multitap.