A collection of handy utilities.
npm install amp-utilsconst amp = require('amp-utils');
amp.string.slug('This string will be slugged.');Move an item within an array. Returns Array.
array: The targetArrayfrom: The oldIntegerindex of the array itemto: The newIntegerindex of the array item
const breakfast = [
'eggs',
'toast',
'bacon',
];
amp.array.move(breakfast, 2, 0); // ['bacon', 'eggs', 'toast']Get unique items from an array. Returns Array.
array: The targetArray
amp.array.unique(['a', 'a', 'b', 'c', 'c']); // ['a', 'b', 'c']Get the closest matching HTML element parent by CSS selector. Returns HTMLElement or null (if no matching parent is found).
start: The startingHTMLElementselector: A valid CSS selectorStringfor matching parent elements
amp.html.closest(document.getElementById('menu'), '.menu-container');Does this DOM element match the provided CSS selector? Returns Boolean.
el: TheHTMLElementfor comparisonselector: A valid CSS selectorStringfor matching
amp.html.matches(document.getElementById('menu'), '.menu');Get or set object value by key path. Returns Object or null if the target object is not found.
search: TheObjectthat will be searchedpath: TheStringpath to the target object or value in dot notation (for example, 'parent.child.grandchild')value: Set target object to this value instead of getting its value
const ride = {
type: 'Truck',
wheels: 4,
passengers: {
driver: {
name: 'Edith',
age: 30,
},
shotgun: {
name: 'Edmund',
age: 29,
}
},
};
amp.object.byPath(ride, 'type'); // 'Truck'
amp.object.byPath(ride, 'passengers.driver.name'); // 'Edith'
amp.object.byPath(ride, 'passengers.shotgun.name', 'Joe'); // Object ('Edmund' is now 'Joe')Clone an object. Returns Object.
obj: TheObjectthat will be cloned
const original = {
a: 'one',
b: {
one: [1, 2, 3],
},
};
const clone = amp.object.clone(original);Compare two Objects for equality. Returns Boolean.
a: The firstObjectfor comparisonb: The secondObjectfor comparison
amp.object.equal({ a: 'A'}, { a: 'A'}); // true
amp.object.equal({ a: 'A' }, { a: 'B' }); // falseIs this an object? Returns Boolean.
obj: TheObjectin question
amp.object.is({ a: 'A' }); // true
amp.object.is('A string!'); // falseDeep merge two or more objects. Returns Object.
target: Properties will be copied into thisObjectsources: One or more sourceObjectsto merge into the target
amp.object.merge({ a: 'A' }, { b: 'B' }); // { a: 'A', b: 'B' }
amp.object.merge({ a: 'A' }, { a: 'B' }); // { a: 'B' }Build a configuration object with default values. Returns Object. This is similar to the amp.object.merge() method, but does not overwrite the default configuration values. Note that you may also use amp.options(), a synonym for this method.
defaultConfig: Default configuration optionsObjectconfig: Configuration optionsObject, will overwrite default options
const getAnimal = (config) => {
const options = amp.object.options({
name: 'Moby',
type: 'dog',
}, config);
return options;
};
getAnimal({ name: 'Mighty' }); // { name: 'Mighty', type: 'dog' }
getAnimal({ name: 'Edith', type: 'cat' }); // { name: 'Edith', type: 'cat' }
getAnimal({ name: 'T-Bone', type: 'bird', age: 3 }); // { name: 'T-Bone', type: 'bird', age: 3 }Parse query string for a parameter value. Returns String or null if no value is found.
uri: The URI or queryStringkey: The query string parameter name (String)
const url = '?name=Edmund&type=cat';
amp.queryString.get(url, 'name'); // 'Edmund'
amp.queryString.get(url, 'type'); // 'cat'Update query string with a new parameter value. Returns String.
uri: The URI or queryStringkey: The query string parameter name (String)value: The new parameter value
const url = '?name=Edmund&type=cat';
amp.queryString.set(url, 'name', 'Edith'); // '?name=Edith&type=cat'
amp.queryString.set(url, 'age', '3'); // '?name=Edmund&type=cat&age=3'Generate a globally unique identifier (GUID) string. Returns String.
amp.string.guid(); // '5aac52a7-d431-aecc-8d35-ef18bf1cbb85'
amp.string.guid(); // 'f2295534-89e3-50cd-18d7-120c62311ac7'Slugify the given string. Returns String.
str: TheStringto slugify
amp.string.slug('Pomp & Circumstance'); // 'pomp-circumstance'Transform a string to title case. Returns String.
str: TheStringto transform
amp.string.titleCase('eine kleine nachtmusik'); // 'Eine Kleine Nachtmusik'Trim slashes from a string or path. Returns String.
path: TheStringthat will be trimmed
amp.string.trimSlashes('/dogs/moby/fetch/'); // 'dogs/moby/fetch'