Utils
Collection of reusable scripts used by AdonisJS core team
This module exports a collection of re-usable utilties to avoid re-writing the same code in every other package. We also include a handful of Lodash utilities, which are used across the AdonisJS packages eco-system.
Version 3.0 breaking changes
The version 3.0 re-format the exports to expose an "helpers" subpath to be used within the AdonisJS apps as well.
The idea is to separate helpers that we need to share with AdonisJS core inside its own module, accessible as @poppinss/utils/build/helpers
.
Inside helpers subpath
Following modules are now moved to a subpath.
MessageBuilder
base64
compose
fsReadAll
interpolate
requireAll
resolveDir
resolveFrom
import {
MessageBuilder,
base64,
compose,
fsReadAll,
interpolate,
requireAll,
resolveDir,
resolveFrom,
safeEqual
} from '@poppinss/utils'
import {
MessageBuilder,
base64,
compose,
fsReadAll,
interpolate,
requireAll,
resolveDir,
resolveFrom,
safeEqual
} from '@poppinss/utils/build/helpers'
randomString
The randomString
is now part of the string
helpers.
import { randomString } from '@poppinss/utils'
randomString(32)
import { string } from '@poppinss/utils/build/helpers'
string.generateRandom(32)
lodash
The following lodash functions have been removed with new alternatives.
snakeCase
camelCase
startCase
import { lodash } from '@poppinss/utils'
lodash.snakeCase()
lodash.camelCase()
lodash.startCase()
import { string } from '@poppinss/utils/build/helpers'
string.snakeCase()
string.camelCase()
string.titleCase()
Table of contents
Installation
Install the package from npm registry as follows:
npm i @poppinss/utils
yarn add @poppinss/utils
and then use it as follows:
import { requireAll } from '@poppinss/utils'
requireAll(__dirname)
Exception
A custom exception class that extends the Error
class to add support for defining status
and error codes
.
import { Exception } from '@poppinss/utils'
throw new Exception('Something went wrong', 500, 'E_RUNTIME_EXCEPTION')
throw new Exception('Route not found', 404, 'E_ROUTE_NOT_FOUND')
esmRequire
Utility to require script files wihtout worrying about CommonJs
and ESM
exports. This is how it works.
- Returns the exported value for
module.exports
. - Returns the default value is an ESM module has
export default
. - Returns all exports if is an ESM module and doesn't have
export default
.
foo.js
module.exports = {
greeting: 'Hello world',
}
foo.default.js
export default {
greeting: 'Hello world',
}
foo.esm.js
export const greeting = {
greeting: 'hello world',
}
import { esmRequire } from '@poppinss/utils'
esmRequire('./foo.js')
esmRequire('./foo.default.js')
esmRequire('./foo.esm.js')
esmResolver
The esmResolver
method works similar to esmRequire
. However, instead of requiring the file, it accepts the object and returns the exported as per the same logic defined above.
import { esmRequire } from '@poppinss/utils'
esmResolver({ greeting: 'hello world' })
esmResolver({
default: { greeting: 'hello world' },
__esModule: true,
})
esmResolver({
greeting: { greeting: 'hello world' },
__esModule: true,
})
Lodash utilities
Lodash itself is a bulky library and most of the times, we don't need all the functions from it.
Also, all of the lodash functions are published as individual modules on npm. However, most of those individual packages are outdated and using them is not an option.
Instead, we decided to pick some individual utilities that we need inside AdonisJS ecosystem and export them from the lodash package, as each function is exposed in its own separate file.
import { lodash } from '@poppinss/utils'
lodash.get({ name: 'virk' }, 'name')
Exported methods
Following is the list of exported helpers.
Safe stringify
Similar to JSON.stringify
, but also handles Circular references by removing them.
import { safeStringify } from '@poppinss/utils'
const o = { b: 1, a: 0 }
o.o = o
console.log(safeStringify(o))
console.log(JSON.stringify(o))
Safe parse
Similar to JSON.parse
, but protects against Prototype Poisoning
import { safeParse } from '@poppinss/utils'
const input = '{ "user": { "__proto__": { "isAdmin": true } } }'
JSON.parse(input)
safeParse(input)
defineStaticProperty
Explicitly define static properties on a class by checking for hasOwnProperty
. In case of inheritance, the properties from the parent class are cloned vs following the prototypal inheritance.
We use/need this copy from parent class behavior a lot in AdonisJS. Here's an example of Lucid models
You create an application wide base model
class AppModel extends BaseModel {
@column.datetime()
public createdAt: DateTime
}
AdonisJS will create the $columnDefinitions
property on the AppModel
class, that holds all the columns
AppModel.$columnDefinitions
Now, lets create another model inheriting the AppModel
class User extends AppModel {
@column()
public id: number
}
As per the Javascript prototypal inheritance. The User
model will not contain the columns from the AppModel
, because we just re-defined the $columnDefinitions
property. However, we don't want this behavior and instead want to copy the columns from the AppModel
and then add new columns to it.
Voila! Use the defineStaticProperty
helper from this class.
class LucidBaseModel {
static boot() {
defineStaticProperty(this, LucidBaseModel, {
propertyName: '$columnDefinitions',
defaultValue: {},
strategy: 'inherit',
})
}
}
The defineStaticProperty
takes a total of three arguments.
- The first argument is always
this
. - The second argument is the root level base class. This will usually be the class exported by your package or module.
- The third argument takes the
propertyName
, defaultValue (in case, there is nothing to copy)
, and the strategy
. - The
inherit
strategy will copy the properties from the base class. - The
define
strategy will always use the defaultValue
to define the property on the class. In other words, there is no copy behavior, but prototypal inheritance chain is also breaked by explicitly re-defining the property.
Helpers
The helpers module is also available in AdonisJS applications as follows:
import { fsReadAll, string, types } from '@ioc:Adonis/Core/Helpers'
The @poppinss/utils
exposes this module as follows
import { fsReadAll, string, types } from '@poppinss/utils/build/helpers'
fsReadAll
A utility to recursively read all script files for a given directory. This method is equivalent to
readdir + recursive + filter (.js, .json, .ts)
.
import { fsReadAll } from '@poppinss/utils/build/helpers'
const files = fsReadAll(__dirname)
You can also define your custom filter function. The filter function must return true
for files to be included.
const files = fsReadAll(__dirname, (file) => {
return file.endsWith('.foo.js')
})
requireAll
Same as fsReadAll
, but instead require the files. Helpful when you want to load all the config files inside a directory on app boot.
import { requireAll } from '@poppinss/utils/build/helpers'
const config = requireAll(join(__dirname, 'config'))
{
file1: {},
file2: {}
}
resolveFrom
Works similar to require.resolve
, however it handles the absolute paths properly.
import { resolveFrom } from '@poppinss/utils/build/helpers'
resolveFrom(__dirname, 'npm-package')
resolveFrom(__dirname, './foo.js')
resolveFrom(__dirname, join(__dirname, './foo.js'))
resolveDir
The require.resolve
or resolveFrom
method can only resolve paths to a given file and not the directory. For example: If you pass path to a directory, then it will search for index.js
inside it and in case of a package, it will be search for main
entry point.
On the other hand, the resolveDir
method can also resolve path to directories using following resolution.
- Absolute paths are returned as it is.
- Relative paths starting with
./
or .\
are resolved using path.join
. - Path to packages inside
node_modules
are resolved as follows: - Uses require.resolve
to resolve the package.json
file. - Then replace the package-name
with the absolute resolved package path.
import { resolveDir } from '@poppinss/utils/build/helpers'
resolveDir(__dirname, './database/migrations')
resolveDir(__dirname, 'some-package/database/migrations')
resolveDir(__dirname, '@some/package/database/migrations')
interpolate
A small utility function to interpolate values inside a string.
import { interpolate } from '@poppinss/utils/build/helpers'
interpolate('hello {{ username }}', {
username: 'virk'
})
interpolate('hello {{ users.0.username }}', {
users: [{ username: 'virk' }]
})
If value is missing, it will be replaced with an 'undefined'
string.
Use the \
to escape a mustache block from getting evaluated.
import { interpolate } from '@poppinss/utils/build/helpers'
interpolate('\\{{ username }} expression evaluates to {{ username }}', {
username: 'virk'
})
Base 64 Encode/Decode
Following helpers for base64 encoding/decoding also exists.
encode
import { base64 } from '@poppinss/utils/build/helpers'
base64.encode('hello world')
base64.encode(Buffer.from('hello world', 'binary'))
decode
import { base64 } from '@poppinss/utils/build/helpers'
base64.decode(base64.encode('hello world'))
base64.decode(base64.encode(Buffer.from('hello world', 'binary')), 'binary')
urlEncode
Same as encode
, but safe for URLS and Filenames
urlDecode
Same as decode
, but decodes the urlEncode
output values
Safe equal
Compares two values by avoid timing attack. Accepts any input that can be passed to Buffer.from
import { safeValue } from '@poppinss/utils/build/helpers'
if (safeValue('foo', 'foo')) {
}
Message Builder
Message builder provides a sane API for stringifying objects similar to JSON.stringify
but has a few advantages.
- It is safe from JSON poisoning vulnerability.
- You can define expiry and purpose for the encoding. The
verify
method will respect these values.
The message builder alone may seem useless, since anyone can decode the object and change its expiry or purpose. However, you can generate an hash of the stringified object and verify the tampering by validating the hash. This is what AdonisJS does for cookies.
import { MessageBuilder } from '@poppinss/utils/build/helpers'
const builder = new MessageBuilder()
const encoded = builder.build({ username: 'virk' }, '1 hour', 'login')
Now verify it
builder.verify(encoded)
builder.verify(encoded, 'register')
builder.verify(encoded, 'login')
compose
Javascript doesn't have a concept of inherting multiple classes together and neither does Typescript. However, the official documentation of Typescript does talks about the concept of mixins.
As per the Typescript docs, you can create and apply mixins as follows.
type Constructor = new (...args: any[]) => any
const UserWithEmail = <T extends Constructor>(superclass: T) => {
return class extends superclass {
public email: string
}
}
const UserWithPassword = <T extends Constructor>(superclass: T) => {
return class extends superclass {
public password: string
}
}
class BaseModel {}
class User extends UserWithPassword(UserWithEmail(BaseModel)) {}
Mixins are close to a perfect way of inherting multiple classes. I recommend reading this article for same.
However, the syntax of applying multiple mixins is kind of ugly, as you have to apply mixins over mixins, creating a nested hierarchy as shown below.
UserWithAttributes(UserWithAge(UserWithPassword(UserWithEmail(BaseModel))))
The compose
method is a small utility to improve the syntax a bit.
import { compose } from '@poppinss/utils/build/helpers'
class User extends compose(
BaseModel,
UserWithPassword,
UserWithEmail,
UserWithAge,
UserWithAttributes
) {}
Mixins gotchas
Typescript has an open issue related to the constructor arguments of the mixin class or the base class.
Typescript expects all classes used in the mixin chain to have a constructor with only one argument of ...args: any[]
. For example: The following code will work fine at runtime, but the typescript compiler complains about it.
class BaseModel {
constructor(name: string) {}
}
const UserWithEmail = <T extends typeof BaseModel>(superclass: T) => {
return class extends superclass {
public email: string
}
}
class User extends compose(BaseModel, UserWithEmail) {}
You can work around this by overriding the constructor of the base class.
import { NormalizeConstructor, compose } from '@poppinss/utils/build/helpers'
const UserWithEmail = <T extends NormalizeConstructor<typeof BaseModel>>(superclass: T) => {
return class extends superclass {
public email: string
}
}
string
The string
module includes a bunch of helper methods to work with strings.
camelCase
Convert a string to its camelCase
version.
import { string } from '@poppinss/utils/build/helpers'
string.camelCase('hello-world')
snakeCase
Convert a string to its snake_case
version.
import { string } from '@poppinss/utils/build/helpers'
string.snakeCase('helloWorld')
dashCase
Convert a string to its dash-case
version. Optionally, you can also capitalize the first letter of each segment.
import { string } from '@poppinss/utils/build/helpers'
string.dashCase('helloWorld')
string.dashCase('helloWorld', { capitalize: true })
pascalCase
Convert a string to its PascalCase
version.
import { string } from '@poppinss/utils/build/helpers'
string.pascalCase('helloWorld')
capitalCase
Capitalize a string
import { string } from '@poppinss/utils/build/helpers'
string.capitalCase('helloWorld')
sentenceCase
Convert string to a sentence
import { string } from '@poppinss/utils/build/helpers'
string.sentenceCase('hello-world')
dotCase
Convert string to its dot.case
version.
import { string } from '@poppinss/utils/build/helpers'
string.dotCase('hello-world')
noCase
Remove all sorts of casing
import { string } from '@poppinss/utils/build/helpers'
string.noCase('hello-world')
string.noCase('hello_world')
string.noCase('helloWorld')
titleCase
Convert a sentence to title case
import { string } from '@poppinss/utils/build/helpers'
string.titleCase('Here is a fox')
pluralize
Pluralize a word.
import { string } from '@poppinss/utils/build/helpers'
string.pluralize('box')
string.pluralize('i')
You can also define your own irregular rules using the string.defineIrregularRule
method.
- The first argument is the singular variation
- The second argument is the plural variation
import { string } from '@poppinss/utils/build/helpers'
string.defineIrregularRule('auth', 'auth')
string.plural('auth')
You can also define your own uncountable rules using the string.defineUncountableRule
method.
import { string } from '@poppinss/utils/build/helpers'
string.defineUncountableRule('login')
string.plural('login')
truncate
Truncate a string after a given number of characters
import { string } from '@poppinss/utils/build/helpers'
string.truncate(
'This is a very long, maybe not that long title',
12
)
By default, the string is truncated exactly after the given characters. However, you can instruct the method to wait for the words to complete.
string.truncate(
'This is a very long, maybe not that long title',
12,
{
completeWords: true
}
)
Also, it is possible to customize the suffix.
string.truncate(
'This is a very long, maybe not that long title',
12,
{
completeWords: true,
suffix: ' <a href="/1"> Read more </a>',
}
)
excerpt
The excerpt
method is same as the truncate
method. However, it strips the HTML from the string.
import { string } from '@poppinss/utils/build/helpers'
string.excerpt(
'<p>This is a <strong>very long</strong>, maybe not that long title</p>',
12
)
condenseWhitespace
Condense whitespaces from a given string. The method removes the whitespace from the left
, right
and multiple whitespace in between the words.
import { string } from '@poppinss/utils/build/helpers'
string.condenseWhitespace(' hello world ')
escapeHTML
Escape HTML from the string
import { string } from '@poppinss/utils/build/helpers'
string.escapeHTML('<p> foo © bar </p>')
Additonally, you can also encode non-ascii symbols
import { string } from '@poppinss/utils/build/helpers'
string.escapeHTML(
'<p> foo © bar </p>',
{
encodeSymbols: true
}
)
encodeSymbols
Encode symbols. Checkout he for available options
import { string } from '@poppinss/utils/build/helpers'
string.encodeSymbols('foo © bar')
toSentence
Join an array of words with a separator.
import { string } from '@poppinss/utils/build/helpers'
string.toSentence([
'route',
'middleware',
'controller'
])
string.toSentence([
'route',
'middleware'
])
You can also customize
separator
: The value between two words except the last onepairSeparator
: The value between the first and the last word. Used, only when there are two wordslastSeparator
: The value between the second last and the last word. Used, only when there are more than two words
string.toSentence([
'route',
'middleware',
'controller'
], {
separator: '/ ',
lastSeparator: '/or '
})
prettyBytes
Convert bytes value to a human readable string. For options, recommend the bytes package.
import { string } from '@poppinss/utils/build/helpers'
string.prettyBytes(1024)
string.prettyBytes(1024, { unitSeparator: ' ' })
toBytes
Convert human readable string to bytes. This method is the opposite of the prettyBytes
method.
import { string } from '@poppinss/utils/build/helpers'
string.toBytes('1KB')
prettyMs
Convert time in milliseconds to a human readable string
import { string } from '@poppinss/utils/build/helpers'
string.prettyMs(60000)
string.prettyMs(60000, { long: true })
toMs
Convert human readable string to milliseconds. This method is the opposite of the prettyMs
method.
import { string } from '@poppinss/utils/build/helpers'
string.toMs('1min')
ordinalize
Ordinalize a string or a number value
import { string } from '@poppinss/utils/build/helpers'
string.ordinalize(1)
string.ordinalize(99)
generateRandom
Generate a cryptographically strong random string
import { string } from '@poppinss/utils/build/helpers'
string.generateRandom(32)
isEmpty
Find if a value is empty. Also checks for empty strings with all whitespace
import { string } from '@poppinss/utils/build/helpers'
string.isEmpty('')
string.isEmpty(' ')
Types
The types module allows distinguishing between different Javascript datatypes. The typeof
returns the same type for many different values. For example:
typeof ({})
typeof ([])
typeof (null)
WHAT??? Yes, coz everything is an object in Javascript. To have better control, you can make use of the types.lookup
method.
lookup
Returns a more accurate type for a given value.
import { types } from '@poppinss/utils/build/helpers'
types.lookup({})
types.lookup([])
types.lookup(Object.create(null))
types.lookup(null)
types.lookup(function () {})
types.lookup(class Foo {})
types.lookup(new Map())
isNull
Find if the given value is null
import { types } from '@poppinss/utils/build/helpers'
types.isNull(null))
isBoolean
Find if the given value is a boolean
import { types } from '@poppinss/utils/build/helpers'
types.isBoolean(true))
isBuffer
Find if the given value is a buffer
import { types } from '@poppinss/utils/build/helpers'
types.isBuffer(new Buffer()))
isNumber
Find if the given value is a number
import { types } from '@poppinss/utils/build/helpers'
types.isNumber(100))
isString
Find if the given value is a string
import { types } from '@poppinss/utils/build/helpers'
types.isString('hello'))
isArguments
Find if the given value is an arguments object
import { types } from '@poppinss/utils/build/helpers'
function foo() {
types.isArguments(arguments))
}
isObject
Find if the given value is a plain object
import { types } from '@poppinss/utils/build/helpers'
types.isObject({}))
isDate
Find if the given value is a date object
import { types } from '@poppinss/utils/build/helpers'
types.isDate(new Date()))
isArray
Find if the given value is an array
import { types } from '@poppinss/utils/build/helpers'
types.isArray([1, 2, 3]))
isRegexp
Find if the given value is an regular expression
import { types } from '@poppinss/utils/build/helpers'
types.isRegexp(/[a-z]+/))
isError
Find if the given value is an instance of the error object
import { types } from '@poppinss/utils/build/helpers'
import { Exception } from '@poppinss/utils'
types.isError(new Error('foo')))
types.isError(new Exception('foo')))
isFunction
Find if the given value is a function
import { types } from '@poppinss/utils/build/helpers'
types.isFunction(function foo() {}))
isClass
Find if the given value is a class constructor. Uses regex to distinguish between a function and a class.
import { types } from '@poppinss/utils/build/helpers'
class User {}
types.isClass(User)
types.isFunction(User)
isInteger
Find if the given value is an integer.
import { types } from '@poppinss/utils/build/helpers'
types.isInteger(22.00)
types.isInteger(22)
types.isInteger(-1)
types.isInteger(-1.00)
types.isInteger(22.10)
types.isInteger(.3)
types.isInteger(-.3)
isFloat
Find if the given value is an float number.
import { types } from '@poppinss/utils/build/helpers'
types.isFloat(22.10)
types.isFloat(-22.10)
types.isFloat(.3)
types.isFloat(-.3)
types.isFloat(22.00)
types.isFloat(-22.00)
types.isFloat(-22)
isDecimal
Find if the given value has a decimal. The value can be a string or a number. The number values are casted to a string by calling the toString()
method on the value itself.
The string conversion is peformed to test the value against a regex. Since, there is no way to natively find a decimal value in Javascript.
import { types } from '@poppinss/utils/build/helpers'
types.isDecimal('22.10')
types.isDecimal(22.1)
types.isDecimal('-22.10')
types.isDecimal(-22.1)
types.isDecimal('.3')
types.isDecimal(0.3)
types.isDecimal('-.3')
types.isDecimal(-0.3)
types.isDecimal('22.00')
types.isDecimal(22.0)
types.isDecimal('-22.00')
types.isDecimal(-22.0)
types.isDecimal('22')
types.isDecimal(22)
types.isDecimal('0.0000000000001')
types.isDecimal(0.0000000000001)
ObjectBuilder
A very simple class to conditionally builder an object. Quite often, I create a new object from an existing one and wants to avoid writing undefined values to it. For example
const obj = {
...(user.username ? { username: user.username } : {}),
...(user.id ? { id: user.id } : {}),
...(user.createdAt ? { createdAt: user.createdAt.toString() } : {}),
}
Not only the above code is harder to write. It is performance issues as well, since we are destructuring too many objects.
To address this use case, you can make use of the ObjectBuilder
class as follows
import { ObjectBuilder } from '@poppinss/utils/build/helpers'
const obj = new ObjectBuilder()
.add('username', user.username)
.add('id', user.id)
.add('createdAt', user.createdAt && user.createdAt.toString())
.value
The add
method ignores the value if its undefined
. So it never gets added to the object at all. You can also ignore null
properties by passing a boolean flag to the constructor.
new ObjectBuilder(true)