Methods

arrangeArray(inputs, trimItemopt) → {Array.<string>}

Reorders items by handling '^' (move to front) and '$' (move to end) prefixes.

Parameters:
NameTypeAttributesDefaultDescription
inputsArray.<string>

Source array.

trimItemboolean<optional>
true

Trim each item before processing.

Returns:

Reordered items.

Type: 
Array.<string>

createClass(className, propertiesopt, parentopt) → {function}

Creates a dynamic class and optionally extends a parent class.

Parameters:
NameTypeAttributesDefaultDescription
classNamestring

Class name hint.

propertiesObject<optional>
{}

Default properties assigned to instances.

parentfunction<optional>

Parent class to extend.

Returns:

Generated class constructor.

Type: 
function

currentLoc(metaImport) → {Object}

Get current directory & file. An attempt to brings back the old __dirname and __filename to the ES6 era.

Returns object with the following keys:

  • __dirname (aliases to dir) - current directory name
  • __filename (aliases to file) - current file path

Example:

const { importModule } = this.app.bajo
const currentLoc = await importModule('bajo:/lib/current-loc.js')

const { __dirname, __filename } = currentLoc(import.meta)
console.log(__dirname, __filename)
Parameters:
NameTypeDescription
metaImportObject

import.meta object

Returns:
Type: 
Object

defaultsDeep(…args) → {Object}

Deep-merges objects while replacing arrays from higher-priority inputs.

Parameters:
NameTypeAttributesDescription
argsObject<repeatable>

Objects to merge.

Returns:

Merged object.

Type: 
Object

delay(msopt) → {Promise.<void>}

Waits for a duration and resolves a promise.

Parameters:
NameTypeAttributesDefaultDescription
msnumber<optional>
1000

Delay in milliseconds.

Returns:

Promise resolved after the delay.

Type: 
Promise.<void>

extractText(text, patternStart, patternEnd) → {Object}

Extracts text between two boundary patterns.

Parameters:
NameTypeDescription
textstring

Source text.

patternStartstring

Start marker.

patternEndstring

End marker.

Returns:

Extracted value and matched pattern.

Type: 
Object

fillObject(obj, keys, defValue) → {Object}

Fill object with keys. defValue is used if the corresponding key doesn't have value

Parameters:
NameTypeDescription
objObject

Object to fill

keysArray.<string>

Array of keys

defValueany

Default value

Returns:
Type: 
Object

filterDuplicate(inputs, key)

Filter array of duplicates

Parameters:
NameTypeDescription
inputsArray

Array to filter from. Must be an array of objects

keystring

Key's object

filterIndex(inputs, matcher) → {Array}

Filter array's indexes

Parameters:
NameTypeDescription
inputsArray

Array to filter from

matcherObject

Function to match conditions. If returns true, the running index will be added to the result

Returns:

Filtered indexes

Type: 
Array

formatText(text, …args) → {string}

Format text according using sprintf with extra ability to run its arguments through a serie of modifiers Modifier must be place right after a token prepended with pipe ``|``` symbols. You can put as many mofifiers as you want.

E.g:

const text = 'Collected %s|toLower|upperFirst: %d'
const result = this.app.lib.formatText(text, 'iTeMs', 10)
console.log(result) // Collected Items: 10
Parameters:
NameTypeAttributesDescription
textstring

Text to be formatted

argsany<repeatable>

Argumennts

Returns:

Formatted text

Type: 
string

getCallerFilename() → {string|undefined}

Returns the caller file path from the current stack trace.

Returns:

Caller file path.

Type: 
string | undefined

getIndex(inputs, matcher) → {Array.<number>}

Returns indexes of items that match a predicate.

Parameters:
NameTypeDescription
inputsArray.<any>

Input array.

matcherfunction

Predicate function.

Returns:

Matching indexes.

Type: 
Array.<number>

getKeyByValue(object, value) → {string|undefined}

Finds an object key by value.

Parameters:
NameTypeDescription
objectObject

Source object.

valueany

Value to find.

Returns:

Matching key.

Type: 
string | undefined

includes(matcheropt, arrayopt) → {boolean}

Checks whether at least one matcher value exists in an array.

Parameters:
NameTypeAttributesDefaultDescription
matcherstring | Array.<string><optional>
[]

Single matcher or list of matchers.

arrayArray.<string><optional>
[]

Array to inspect.

Returns:

True when any matcher is found.

Type: 
boolean

isClass(item) → {boolean}

Checks whether a value appears to be a class constructor.

Parameters:
NameTypeDescription
itemany

Value to test.

Returns:

True when item is class-like.

Type: 
boolean

isSet(item) → {boolean}

Checks whether a value is neither null nor undefined.

Parameters:
NameTypeDescription
itemany

Value to test.

Returns:

True when value is set.

Type: 
boolean

mergeObjectsByKey(inputs, key) → {Array.<Object>}

Merges objects by a shared key, where later objects override earlier fields.

Parameters:
NameTypeDescription
inputsArray.<Object>

Source objects.

keystring

Field name used for grouping.

Returns:

Merged objects.

Type: 
Array.<Object>

paginate(collectionopt, optionsopt) → {Object|Array.<Object>|number|number|number|number}

Paginates and optionally sorts a collection.

Parameters:
NameTypeAttributesDefaultDescription
collectionArray.<Object><optional>
[]

Source collection.

optionsObject<optional>
{}

Pagination options.

Properties
NameTypeAttributesDefaultDescription
pagenumber<optional>
1

1-based page number.

limitnumber<optional>
25

Items per page.

sortObject.<string, number><optional>

Sort descriptor where value < 0 means descending.

Returns:
  • Paginated result.

    Type: 
    Object
  • return.data Sliced page data.

    Type: 
    Array.<Object>
  • return.page Current page number.

    Type: 
    number
  • return.limit Page size.

    Type: 
    number
  • return.count Total items in source collection.

    Type: 
    number
  • return.pages Total page count.

    Type: 
    number

(async) parseArgsArgv(optionsopt) → {Object}

Parse program arguments (args) & options (argv)

Parameters:
NameTypeAttributesDefaultDescription
optionsObject<optional>
{}

Options object

Properties
NameTypeAttributesDescription
sanitizerfunction<optional>

If provided, result will be sanitized with this function

useParserboolean<optional>

If true, skip yargs

cwdstring<optional>

If provided, current working directory is set to this

See
  • App#args
  • App#argv
Returns:

An object containing args and argv

Type: 
Object

parseDuration(dur) → {number}

Parse duration to its millisecond value. Use ms under the hood

Parameters:
NameTypeDescription
durnumber | string

If string is given, parse this to its millisecond value. Otherwise returns as is

See
Returns:
Type: 
number

parseEnv(sanitizeropt) → {Object}

Parse environment variables

Parameters:
NameTypeAttributesDescription
sanitizerfunction<optional>

If provided, result will be sanitized with this function

Returns:
Type: 
Object

parseKvString(text, trimValueopt, lineDelimiteropt) → {Object}

Parses key-value text into an object and normalizes primitive types.

Parameters:
NameTypeAttributesDefaultDescription
textstring

Input key-value string.

trimValueboolean<optional>
true

Trim value parts.

lineDelimiterstring<optional>
'|'

Delimiter between lines.

Returns:

Parsed object.

Type: 
Object

parseObject(input, optionsopt) → {Object}

Parses an object (or JSON string) and recursively normalizes nested values.

When options.parseValue is enabled, string values are converted using dotenv-parse-variables rules (for example booleans, numbers, arrays, and null), including values inside arrays and nested objects.

Special key handling:

  • Keys ending with Dur are converted to milliseconds using parseDuration.
  • Keys ending with Dt are converted to Unix timestamps using Date.parse.
  • options.translator can translate prefixed keys/values before normalization.
Parameters:
NameTypeAttributesDefaultDescription
inputObject | string

If string is given, parse it first using JSON.parse.

optionsObject<optional>
{}

Options.

Properties
NameTypeAttributesDefaultDescription
silentboolean<optional>
true

If true, parsing errors are ignored and values become undefined.

parseValueboolean<optional>
false

If true, values are parsed and normalized.

translatorObject<optional>

Optional translator config for prefixed keys/values.

Returns:

Parsed and normalized object.

Type: 
Object

pascalCase(text) → {string}

Converts text into PascalCase.

Parameters:
NameTypeDescription
textstring

Source text.

Returns:

Pascal-cased text.

Type: 
string

randomRange(min, max, alpha) → {number|string}

Generate random value.

Parameters:
NameTypeDefaultDescription
minnumber0

Minimum value (inclusive).

maxnumber65439

Maximum value (inclusive).

alphabooleanfalse

Set true to return alphanumeric characters.

Returns:

Random value.

Type: 
number | string

resolvePath(file, asFileUrlopt) → {string}

Resolve file name to filesystem's path. Windows path separator \ is normalized to Unix's /

Parameters:
NameTypeAttributesDefaultDescription
filestring

File to resolve.

asFileUrlboolean<optional>
false

Return as file URL format file:///<name>.

Returns:
Type: 
string

round(val, scaleopt) → {number}

Rounds a number using decimal precision.

Parameters:
NameTypeAttributesDefaultDescription
valnumber

Number to round.

scalenumber<optional>
0

Decimal places.

Returns:

Rounded number.

Type: 
number

(async) setImmediate() → {Promise.<void>}

Schedules execution on the next immediate cycle and resolves when it runs.

Returns:

Promise resolved on the immediate queue.

Type: 
Promise.<void>

shim() → {void}

Attaches missing String polyfills when they are not available in the current runtime. Adds String.prototype.replaceAll and String.prototype.splice if needed.

Returns:
Type: 
void

textToArray(text) → {Array.<string>}

Converts multiline text into a cleaned array and removes comments.

Parameters:
NameTypeDescription
textstring

Input text.

Returns:

Cleaned lines.

Type: 
Array.<string>

titleize(textopt, optionsopt) → {string}

Converts text to title case with optional ignore and replacement rules.

Parameters:
NameTypeAttributesDefaultDescription
textstring<optional>
''

Source text.

optionsObject<optional>
{}

Transformation options.

Properties
NameTypeAttributesDefaultDescription
ignoresArray.<string><optional>
[]

Words that should remain unchanged.

replacementObject.<string, string><optional>
{}

Exact token replacements.

Returns:

Titleized text.

Type: 
string

without(arropt, …params) → {Array.<any>}

Returns an array without provided values and empty-like entries.

Parameters:
NameTypeAttributesDefaultDescription
arrArray.<any><optional>
[]

Source array.

paramsany<repeatable>

Values to exclude.

Returns:

Filtered array.

Type: 
Array.<any>
Example
without([1, 2, null, '', 3, 2], 2)
// => [1, 3]