Methods
arrangeArray(inputs, trimItemopt) → {Array.<string>}
Reorders items by handling '^' (move to front) and '$' (move to end) prefixes.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
inputs | Array.<string> | Source array. | ||
trimItem | boolean | <optional> | true | Trim each item before processing. |
- Source
Reordered items.
- Type:
- Array.<string>
createClass(className, propertiesopt, parentopt) → {function}
Creates a dynamic class and optionally extends a parent class.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
className | string | Class name hint. | ||
properties | Object | <optional> | {} | Default properties assigned to instances. |
parent | function | <optional> | Parent class to extend. |
- Source
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 todir) - current directory name__filename(aliases tofile) - 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)
| Name | Type | Description |
|---|---|---|
metaImport | Object |
|
- Source
- Type:
- Object
defaultsDeep(…args) → {Object}
Deep-merges objects while replacing arrays from higher-priority inputs.
| Name | Type | Attributes | Description |
|---|---|---|---|
args | Object | <repeatable> | Objects to merge. |
- Source
Merged object.
- Type:
- Object
delay(msopt) → {Promise.<void>}
Waits for a duration and resolves a promise.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
ms | number | <optional> | 1000 | Delay in milliseconds. |
Promise resolved after the delay.
- Type:
- Promise.<void>
extractText(text, patternStart, patternEnd) → {Object}
Extracts text between two boundary patterns.
| Name | Type | Description |
|---|---|---|
text | string | Source text. |
patternStart | string | Start marker. |
patternEnd | string | End marker. |
- Source
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
| Name | Type | Description |
|---|---|---|
obj | Object | Object to fill |
keys | Array.<string> | Array of keys |
defValue | any | Default value |
- Source
- Type:
- Object
filterDuplicate(inputs, key)
Filter array of duplicates
| Name | Type | Description |
|---|---|---|
inputs | Array | Array to filter from. Must be an array of objects |
key | string | Key's object |
- Source
filterIndex(inputs, matcher) → {Array}
Filter array's indexes
| Name | Type | Description |
|---|---|---|
inputs | Array | Array to filter from |
matcher | Object | Function to match conditions. If returns |
- Source
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
| Name | Type | Attributes | Description |
|---|---|---|---|
text | string | Text to be formatted | |
args | any | <repeatable> | Argumennts |
- Source
Formatted text
- Type:
- string
getCallerFilename() → {string|undefined}
Returns the caller file path from the current stack trace.
- Source
Caller file path.
- Type:
- string |
undefined
getIndex(inputs, matcher) → {Array.<number>}
Returns indexes of items that match a predicate.
| Name | Type | Description |
|---|---|---|
inputs | Array.<any> | Input array. |
matcher | function | Predicate function. |
- Source
Matching indexes.
- Type:
- Array.<number>
getKeyByValue(object, value) → {string|undefined}
Finds an object key by value.
| Name | Type | Description |
|---|---|---|
object | Object | Source object. |
value | any | Value to find. |
- Source
Matching key.
- Type:
- string |
undefined
includes(matcheropt, arrayopt) → {boolean}
Checks whether at least one matcher value exists in an array.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
matcher | string | | <optional> | [] | Single matcher or list of matchers. |
array | Array.<string> | <optional> | [] | Array to inspect. |
- Source
True when any matcher is found.
- Type:
- boolean
isClass(item) → {boolean}
Checks whether a value appears to be a class constructor.
| Name | Type | Description |
|---|---|---|
item | any | Value to test. |
- Source
True when item is class-like.
- Type:
- boolean
isSet(item) → {boolean}
Checks whether a value is neither null nor undefined.
| Name | Type | Description |
|---|---|---|
item | any | Value to test. |
True when value is set.
- Type:
- boolean
mergeObjectsByKey(inputs, key) → {Array.<Object>}
Merges objects by a shared key, where later objects override earlier fields.
| Name | Type | Description |
|---|---|---|
inputs | Array.<Object> | Source objects. |
key | string | Field name used for grouping. |
- Source
Merged objects.
- Type:
- Array.<Object>
paginate(collectionopt, optionsopt) → {Object|Array.<Object>|number|number|number|number}
Paginates and optionally sorts a collection.
| Name | Type | Attributes | Default | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
collection | Array.<Object> | <optional> | [] | Source collection. | ||||||||||||||||||||
options | Object | <optional> | {} | Pagination options. Properties
|
- Source
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)
| Name | Type | Attributes | Default | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | Object | <optional> | {} | Options object Properties
|
- Source
- See
- App#args
- App#argv
An object containing args and argv
- Type:
- Object
parseDuration(dur) → {number}
Parse duration to its millisecond value. Use ms under the hood
| Name | Type | Description |
|---|---|---|
dur | number | | If string is given, parse this to its millisecond value. Otherwise returns as is |
- Source
- See
- Type:
- number
parseEnv(sanitizeropt) → {Object}
Parse environment variables
| Name | Type | Attributes | Description |
|---|---|---|---|
sanitizer | function | <optional> | If provided, result will be sanitized with this function |
- Source
- Type:
- Object
parseKvString(text, trimValueopt, lineDelimiteropt) → {Object}
Parses key-value text into an object and normalizes primitive types.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
text | string | Input key-value string. | ||
trimValue | boolean | <optional> | true | Trim value parts. |
lineDelimiter | string | <optional> | '|' | Delimiter between lines. |
- Source
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
Durare converted to milliseconds using parseDuration. - Keys ending with
Dtare converted to Unix timestamps using Date.parse. options.translatorcan translate prefixed keys/values before normalization.
| Name | Type | Attributes | Default | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
input | Object | | If string is given, parse it first using JSON.parse. | ||||||||||||||||||||||
options | Object | <optional> | {} | Options. Properties
|
- Source
Parsed and normalized object.
- Type:
- Object
pascalCase(text) → {string}
Converts text into PascalCase.
| Name | Type | Description |
|---|---|---|
text | string | Source text. |
- Source
Pascal-cased text.
- Type:
- string
randomRange(min, max, alpha) → {number|string}
Generate random value.
| Name | Type | Default | Description |
|---|---|---|---|
min | number | 0 | Minimum value (inclusive). |
max | number | 65439 | Maximum value (inclusive). |
alpha | boolean | false | Set |
- Source
Random value.
- Type:
- number |
string
resolvePath(file, asFileUrlopt) → {string}
Resolve file name to filesystem's path. Windows path separator \ is normalized to Unix's /
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
file | string | File to resolve. | ||
asFileUrl | boolean | <optional> | false | Return as file URL format |
- Source
- Type:
- string
round(val, scaleopt) → {number}
Rounds a number using decimal precision.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
val | number | Number to round. | ||
scale | number | <optional> | 0 | Decimal places. |
Rounded number.
- Type:
- number
(async) setImmediate() → {Promise.<void>}
Schedules execution on the next immediate cycle and resolves when it runs.
- Source
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.
- Type:
- void
textToArray(text) → {Array.<string>}
Converts multiline text into a cleaned array and removes comments.
| Name | Type | Description |
|---|---|---|
text | string | Input text. |
- Source
Cleaned lines.
- Type:
- Array.<string>
titleize(textopt, optionsopt) → {string}
Converts text to title case with optional ignore and replacement rules.
| Name | Type | Attributes | Default | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
text | string | <optional> | '' | Source text. | |||||||||||||||
options | Object | <optional> | {} | Transformation options. Properties
|
- Source
Titleized text.
- Type:
- string
without(arropt, …params) → {Array.<any>}
Returns an array without provided values and empty-like entries.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
arr | Array.<any> | <optional> | [] | Source array. |
params | any | <repeatable> | Values to exclude. |
- Source
Filtered array.
- Type:
- Array.<any>
without([1, 2, null, '', 3, 2], 2)
// => [1, 3]