What is @poppinss/utils?
@poppinss/utils is a utility library that provides a collection of helper functions for various common tasks in JavaScript and TypeScript. It includes utilities for data manipulation, type checking, and more.
What are @poppinss/utils's main functionalities?
String Manipulation
The string manipulation utilities include functions like camelCase, which converts a string to camel case.
const { string } = require('@poppinss/utils');
const camelCaseString = string.camelCase('hello world');
console.log(camelCaseString); // 'helloWorld'
Object Utilities
Object utilities include functions like clone, which creates a deep copy of an object.
const { object } = require('@poppinss/utils');
const obj = { a: 1, b: 2 };
const clonedObj = object.clone(obj);
console.log(clonedObj); // { a: 1, b: 2 }
Type Checking
Type checking utilities include functions like is.number and is.string, which check the type of a given value.
const { is } = require('@poppinss/utils');
console.log(is.number(123)); // true
console.log(is.string(123)); // false
Other packages similar to @poppinss/utils
lodash
Lodash is a popular utility library that provides a wide range of functions for data manipulation, type checking, and more. It is more comprehensive and widely used compared to @poppinss/utils.
underscore
Underscore is another utility library similar to Lodash, offering a variety of functions for common programming tasks. It is older and less feature-rich than Lodash but still widely used.
ramda
Ramda is a functional programming library for JavaScript that provides utility functions with a focus on immutability and pure functions. It offers a different approach compared to @poppinss/utils, emphasizing functional programming paradigms.
Utils
This module exports a collection of re-usable utilties to avoid re-writing the same code in every other package. Do make sure to check the API docs as well.
Table of contents
Usage
Install the package from npm as follows:
npm i @poppinss/utils
yarn add @poppinss/utils
and then use it as follows
import { Exception, requireAll, esmRequire } from '@poppinss/utils'
The module exports the following methods/classes.
Exception
Raise an exception with custom status code and error code.
import { Exception } from '@poppinss/utils'
new Exception('Route not found', 404, 'E_ROUTE_NOT_FOUND')
esmRequire
Require a module and returns the default
export if module is an esModule
or returns the actual value in case of CommonJs.
The method is helpful when you don't know that you are requiring a CommonJs module or a ESM module.
TS module (user.ts)
export default {
username: 'foo'
}
import { esmRequire } from '@poppinss/utils'
esmRequire('./user')
requireAll
Require all .js
and .json
and .ts
files from a given directory.
import { join } from 'path'
import { requireAll } from '@poppinss/utils'
requireAll(join(__dirname, 'config'))
requireAll(join(__dirname, 'config'), false)
requireAll(join(__dirname, 'config'), true, true)
parseIoCReference
Through out the AdonisJs applications, one can define IoC container reference as a string and the framework will locate the correct class & method from that reference.
To avoid repeating the logic of parsing the IoC container string reference, we have created a utility method for same.
Route.get('users', 'UserController.index')
In the above code, the Route let's the end user define the Controller.index as a reference to UserController
class. The route uses parseIoCReference
method to pull the actual class from the container as follows.
const output = parseIoCReference('UserController.index', 'App/Controllers/Http')
Also, you can pull the actual object for the given namespace as follows.
const output = parseIoCReference('UserController.index', 'App/Controllers/Http', undefined, true)
Argument | Required | Description |
---|
reference (String) | ✔ | The string reference to the container |
namespace (String) | ✖︎ | An optional namespace to prefix. The namespace is only prefixed when reference is not absolute |
fallbackHandler (String) | ✖ | The fallbackHandler is used when the reference string doesn't defines a method after the . |
eagerLoad (Boolean) | ✖ | Attempt to eagerload the reference from the container |
callIocReference
Invoke the method under a given namespace with automatic depedency injection. This method excepts the application is using @adonisjs/fold
with it's defined globals.
const parsed = parseIocReference('UserController.index')
console.log(callIocReference(parsed, []))
The callIocReference
method uses ioc.make
and ioc.call
and both of these methods support automatic dependency injection.
API
Following are the autogenerated files via Typedoc
Maintainers
Harminder virk