Methods
buildUrlParamObj(url, paramChar) → {object}
Builds an object composed of all query params in a specified URL.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
url |
string
|
Url containing params |
paramChar |
string
|
The character that indicates the start of the query parameters |
Returns:
- Type:
-
object
- Returns an object with the query params and their associated values.
Examples
Builds a params object from a URL
var { buildUrlParamObj } = require('@bowtie/utils')
var url = 'www.example.com/something?key1=value1&key2=value2'
var paramChar = "?"
var result = buildUrlParamObj(url, paramChar)
console.log(result)
// { key1: "value1", key2: "value2" }
Build a params object from a URL with a custom paramChar
var { buildUrlParamObj } = require('@bowtie/utils')
var url = 'www.example.com/something#key1=value1&key2=value2'
var paramChar = '#'
console.log( buildUrlParamObj(url, paramChar) )
// { key1: "value1", key2: "value2" }
capitalizeWord(str) → {string}
Capitalize a string
- Source:
Parameters:
Name | Type | Description |
---|---|---|
str |
string
|
Input string to be capitalized |
Returns:
- Type:
-
string
- Returns capitalized string
Examples
Capitalize a lower case string.
var { capitalizeWord } = require('@bowtie/utils')
var word = capitalizeWord('hello') // word = "Hello"
Capitalize a mixed case string.
var { capitalizeWord } = require('@bowtie/utils')
var word = capitalizeWord('HeLLo') // word = "Hello"
endsWith(subject, search) → {boolean}
Check if subject ends with search string(s)
- Source:
Parameters:
Name | Type | Description |
---|---|---|
subject |
string
|
Subject to search against |
search |
string
|
Array.<string>
|
String or array of strings to check for ending |
Returns:
- Type:
-
boolean
- Returns whether of not
subject
ends withsearch
Examples
Check for single string ending.
var { endsWith } = require('@bowtie/utils')
var sub = 'hello world'
endsWith(sub, 'world') // true
endsWith(sub, 'hello') // false
Check against list of string endings.
var { endsWith } = require('@bowtie/utils')
var sub = 'hello world'
endsWith(sub, [ 'planet', 'world' ]) // true
endsWith(sub, [ 'planet', 'earth' ]) // false
extractUrlParam(url, name) → {string}
Extracts the value of a specified query param from a URL.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
url |
string
|
Url containing params |
name |
string
|
the name of the url param to extract. |
Returns:
- Type:
-
string
- Returns the value of the query param.
Examples
Extracts the value of a specified query param from a URL.
var { extractUrlParam } = require('@bowtie/utils')
var url = 'www.example.com/something?key1=value1&key2=value2'
console.log( extractUrlParam(url, 'key2') )
// logs 'value2'
Build a params object from a URL with a custom paramChar
var { extractUrlParam } = require('@bowtie/utils')
var url = 'www.example.com/something?key1=value1&key2=value2'
console.log( extractUrlParam(url, 'key1') )
// logs 'value1'
startsWith(subject, search) → {boolean}
Check if subject starts with search string(s)
- Source:
Parameters:
Name | Type | Description |
---|---|---|
subject |
string
|
Subject to search against |
search |
string
|
Array.<string>
|
String or array of strings to check for beginning |
Returns:
- Type:
-
boolean
- Returns whether of not
subject
starts withsearch
Examples
Check for single string beginning.
var { startsWith } = require('@bowtie/utils')
var sub = 'hello world'
startsWith(sub, 'world') // false
startsWith(sub, 'hello') // true
Check against list of string beginning.
var { startsWith } = require('@bowtie/utils')
var sub = 'hello world'
startsWith(sub, [ 'hello', 'hola' ]) // true
startsWith(sub, [ 'hola', 'hi' ]) // false
titleize(subject, sep, join) → {string}
Capitalize sentence with default seperator.
- Source:
Parameters:
Name | Type | Default | Description |
---|---|---|---|
subject |
string
|
Subject to titleize |
|
sep |
string
|
Seperator to split the string. Defaults to " ". |
|
join |
string
|
Specificed character to join the string. Defaults to " ". |
Returns:
- Type:
-
string
- Titleized string.
Examples
Capitalize sentence with default seperator.
var { titleize } = require('@bowtie/utils')
var str = 'hello world'
titleize(str) // 'Hello World'
Capitalize string with specified seperator
var { titleize } = require('@bowtie/utils')
var str = 'hello_world'
titleize(str, '_') // Hello World
Capitalize string with specified join character
var { titleize } = require('@bowtie/utils')
var str = 'hello_world'
titleize(str, '_', ', ') // Hello, World
Only capitalize words that are not articles.
var { titleize } = require('@bowtie/utils')
var str = 'this is a title'
titleize(str) // This Is a Title
Capitalize article if it is the first word
var { titleize } = require('@bowtie/utils')
var str = 'a title this is'
titleize(str) // A Title This Is
verifyKeys(obj, keys)
Verify that an object contains required keys
- Source:
Parameters:
Name | Type | Description |
---|---|---|
obj |
object
|
Object to verify keys against |
keys |
Array.<string>
|
Array of strings to verify existence of on |
Throws:
-
Missing required key
- Type
-
Error
Example
Verify object contains keys.
var { verifyKeys } = require('@bowtie/utils')
var obj = { a: '1', b: '2', c: '3' }
verifyKeys(obj, [ 'a', 'b' ]) // Does not throw Error
verifyKeys(obj, [ 'c', 'd' ]) // Throws Error('Missing required key: d')
verifyRequired(options, required)
Verify required options against keys or schema object
- Source:
Parameters:
Name | Type | Description |
---|---|---|
options |
object
|
Options to be checked for requirements |
required |
object
|
Array.<string>
|
Either an array of expected keys, or a schema object |
Throws:
-
Invalid Argument
- Type
-
Error
Examples
Verify options against schema.
var { verifyRequired } = require('@bowtie/utils')
var options = {
opt1: 'something',
opt2: true,
opt3: {
str: 'a string',
num: 12345
}
}
var schema = {
opt1: 'string',
opt2: 'boolean',
opt3: {
str: 'string',
num: 'number'
}
}
verifyRequired(options, schema) // Does not throw Error
options.opt2 = 'a string instead of a boolean'
verifyRequired(options, schema) // Throws Error('Invalid type for key: opt2')
Verify options against keys array.
var { verifyRequired } = require('@bowtie/utils')
var options = {
opt1: 'something',
opt2: true,
opt3: {
str: 'a string',
num: 12345
}
}
verifyRequired(options, [ 'opt1', 'opt2', 'opt3' ]) // Does not throw Error
verifyRequired(options, [ 'opt1', 'opt2', 'opt4' ]) // Throws Error('Missing required key: opt4')
verifySchema(obj, schema)
Verify object schema against object schema definition
- Source:
Parameters:
Name | Type | Description |
---|---|---|
obj |
object
|
Object to be checked for valid schema |
schema |
object
|
Array.<string>
|
Schema to be verified |
Throws:
-
-
Invalid/missing object requiring schema
- Type
-
Error
-
-
-
Invalid type for key
- Type
-
Error
-
Example
Verify options against schema.
var { verifySchema } = require('@bowtie/utils')
var options = {
opt1: 'something',
opt2: true,
opt3: {
str: 'a string',
num: 12345
}
}
var schema = {
opt1: 'string',
opt2: 'boolean',
opt3: {
str: 'string',
num: 'number'
}
}
verifySchema(options, schema) // Does not throw Error
options.opt2 = 'a string instead of a boolean'
verifySchema(options, schema) // Throws Error('Invalid type for key: opt2')