
This is a very long, maybe not that long title
', 12) // This is a very... ``` #### condenseWhitespace Condense whitespaces from a given string. The method removes the whitespace from the `left`, `right` and multiple whitespace in between the words. ```ts import { string } from '@poppinss/utils/build/helpers' string.condenseWhitespace(' hello world ') // hello world ``` #### escapeHTML Escape HTML from the string ```ts import { string } from '@poppinss/utils/build/helpers' string.escapeHTML('foo © bar
') // <p> foo © bar </p> ``` Additonally, you can also encode non-ascii symbols ```ts import { string } from '@poppinss/utils/build/helpers' string.escapeHTML('foo © bar
', { encodeSymbols: true, }) // <p> foo © bar </p> ``` #### encodeSymbols Encode symbols. Checkout [he](https://npm.im/he) for available options ```ts import { string } from '@poppinss/utils/build/helpers' string.encodeSymbols('foo © bar') // foo © bar ``` #### toSentence Join an array of words with a separator. ```ts import { string } from '@poppinss/utils/build/helpers' string.toSentence(['route', 'middleware', 'controller']) // route, middleware, and controller string.toSentence(['route', 'middleware']) // route and middleware ``` You can also customize - `separator`: The value between two words except the last one - `pairSeparator`: The value between the first and the last word. Used, only when there are two words - `lastSeparator`: The value between the second last and the last word. Used, only when there are more than two words ```ts string.toSentence(['route', 'middleware', 'controller'], { separator: '/ ', lastSeparator: '/or ', }) // route/ middleware/or controller ``` #### prettyBytes Convert bytes value to a human readable string. For options, recommend the [bytes](https://www.npmjs.com/package/bytes) package. ```ts import { string } from '@poppinss/utils/build/helpers' string.prettyBytes(1024) // 1KB string.prettyBytes(1024, { unitSeparator: ' ' }) // 1 KB ``` #### toBytes Convert human readable string to bytes. This method is the opposite of the `prettyBytes` method. ```ts import { string } from '@poppinss/utils/build/helpers' string.toBytes('1KB') // 1024 ``` #### prettyMs Convert time in milliseconds to a human readable string ```ts import { string } from '@poppinss/utils/build/helpers' string.prettyMs(60000) // 1min string.prettyMs(60000, { long: true }) // 1 minute ``` #### toMs Convert human readable string to milliseconds. This method is the opposite of the `prettyMs` method. ```ts import { string } from '@poppinss/utils/build/helpers' string.toMs('1min') // 60000 ``` #### ordinalize Ordinalize a string or a number value ```ts import { string } from '@poppinss/utils/build/helpers' string.ordinalize(1) // 1st string.ordinalize(99) // 99th ``` #### generateRandom Generate a cryptographically strong random string ```ts 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 ```ts import { string } from '@poppinss/utils/build/helpers' string.isEmpty('') // true string.isEmpty(' ') // true ``` ### Types The types module allows distinguishing between different Javascript datatypes. The `typeof` returns the same type for many different values. For example: ```ts typeof {} // object typeof [] // object typeof null // object ``` 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. ```ts import { types } from '@poppinss/utils/build/helpers' types.lookup({}) // object types.lookup([]) // array types.lookup(Object.create(null)) // object types.lookup(null) // null types.lookup(function () {}) // function types.lookup(class Foo {}) // class types.lookup(new Map()) // map ``` #### isNull Find if the given value is null ```ts import { types } from '@poppinss/utils/build/helpers' types.isNull(null)) // true ``` #### isBoolean Find if the given value is a boolean ```ts import { types } from '@poppinss/utils/build/helpers' types.isBoolean(true)) // true ``` #### isBuffer Find if the given value is a buffer ```ts import { types } from '@poppinss/utils/build/helpers' types.isBuffer(new Buffer())) // true ``` #### isNumber Find if the given value is a number ```ts import { types } from '@poppinss/utils/build/helpers' types.isNumber(100)) // true ``` #### isString Find if the given value is a string ```ts import { types } from '@poppinss/utils/build/helpers' types.isString('hello')) // true ``` #### isArguments Find if the given value is an arguments object ```ts import { types } from '@poppinss/utils/build/helpers' function foo() { types.isArguments(arguments)) // true } ``` #### isObject Find if the given value is a plain object ```ts import { types } from '@poppinss/utils/build/helpers' types.isObject({})) // true ``` #### isDate Find if the given value is a date object ```ts import { types } from '@poppinss/utils/build/helpers' types.isDate(new Date())) // true ``` #### isArray Find if the given value is an array ```ts import { types } from '@poppinss/utils/build/helpers' types.isArray([1, 2, 3])) // true ``` #### isRegexp Find if the given value is an regular expression ```ts import { types } from '@poppinss/utils/build/helpers' types.isRegexp(/[a-z]+/)) // true ``` #### isError Find if the given value is an instance of the error object ```ts import { types } from '@poppinss/utils/build/helpers' import { Exception } from '@poppinss/utils' types.isError(new Error('foo'))) // true types.isError(new Exception('foo'))) // true ``` #### isFunction Find if the given value is a function ```ts import { types } from '@poppinss/utils/build/helpers' types.isFunction(function foo() {})) // true ``` #### isClass Find if the given value is a class constructor. Uses regex to distinguish between a function and a class. ```ts import { types } from '@poppinss/utils/build/helpers' class User {} types.isClass(User) // true types.isFunction(User) // true ``` #### isInteger Find if the given value is an integer. ```ts import { types } from '@poppinss/utils/build/helpers' types.isInteger(22.0) // true types.isInteger(22) // true types.isInteger(-1) // true types.isInteger(-1.0) // true types.isInteger(22.1) // false types.isInteger(0.3) // false types.isInteger(-0.3) // false ``` #### isFloat Find if the given value is an float number. ```ts import { types } from '@poppinss/utils/build/helpers' types.isFloat(22.1) // true types.isFloat(-22.1) // true types.isFloat(0.3) // true types.isFloat(-0.3) // true types.isFloat(22.0) // false types.isFloat(-22.0) // false types.isFloat(-22) // false ``` #### 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. ```ts import { types } from '@poppinss/utils/build/helpers' types.isDecimal('22.10') // true types.isDecimal(22.1) // true types.isDecimal('-22.10') // true types.isDecimal(-22.1) // true types.isDecimal('.3') // true types.isDecimal(0.3) // true types.isDecimal('-.3') // true types.isDecimal(-0.3) // true types.isDecimal('22.00') // true types.isDecimal(22.0) // false (gets converted to 22) types.isDecimal('-22.00') // true types.isDecimal(-22.0) // false (gets converted to -22) types.isDecimal('22') // false types.isDecimal(22) // false types.isDecimal('0.0000000000001') // true types.isDecimal(0.0000000000001) // false (gets converted to 1e-13) ``` ### 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 ```ts 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 ```ts 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 // returns the underlying object ``` 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. ```ts new ObjectBuilder(true) // ignore null as well ``` [gh-workflow-image]: https://img.shields.io/github/workflow/status/poppinss/utils/test?style=for-the-badge [gh-workflow-url]: https://github.com/poppinss/utils/actions/workflows/test.yml "Github action" [typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript [typescript-url]: "typescript" [npm-image]: https://img.shields.io/npm/v/@poppinss/utils.svg?style=for-the-badge&logo=npm [npm-url]: https://npmjs.org/package/@poppinss/utils 'npm' [license-image]: https://img.shields.io/npm/l/@poppinss/utils?color=blueviolet&style=for-the-badge [license-url]: LICENSE.md 'license' [synk-image]: https://img.shields.io/snyk/vulnerabilities/github/poppinss/utils?label=Synk%20Vulnerabilities&style=for-the-badge [synk-url]: https://snyk.io/test/github/poppinss/utils?targetFile=package.json 'synk'