Just some JS utils
You can use brabbelback in your project by installing it using npm:
npm i @schascha/brabbelback
Finds a value in array recursively.
import { has } from '@schascha/brabbelback';
const array = ['foo', 'bar', 'baz'];
console.log(has(array, 'foo')); // true
console.log(has(array, ['foo', 'bar'])); // true
Checks if value or array is empty.
import { isEmpty } from '@schascha/brabbelback';
console.log(isEmpty(null)); // true
console.log(isEmpty('')); // true
console.log(isEmpty([])); // true
console.log(isEmpty([[]])); // true
Returns last element from array.
import { last } from '@schascha/brabbelback';
const array = ['foo', 'bar', 'baz'];
console.log(last(['foo', 'bar', 'baz'])); // 'baz'
Returns next element from array.
import { next } from '@schascha/brabbelback';
const array = ['foo', 'bar', 'baz'];
console.log(next(0, array)); // 'bar'
console.log(next(2, array)); // 'foo'
console.log(next(2, array, false)); // Disable infinite loop through array. Returns `undefined`.
Returns previous element from array.
import { prev } from '@schascha/brabbelback';
const array = ['foo', 'bar', 'baz'];
console.log(prev(0, array)); // 'baz'
console.log(prev(2, array)); // 'bar'
console.log(prev(0, array, false)); // Disable infinite loop through array. Returns `undefined`.
Prints a value or an array of values.
import { print } from '@schascha/brabbelback';
console.log(print('foo')); // 'foo'
console.log(print(['foo', 'bar', 'baz'])); // 'foo, bar, baz'
console.log(print(['foo', 'bar', 'baz'], '|')); // 'foo|bar|baz'
Pushs a value or an array of values recursively.
import { push } from '@schascha/brabbelback';
const array = [];
push(array, 'foo'); // ['foo']
push(array, ['foo', 'bar', 'baz']); // ['foo', 'bar', 'baz']
push(array, 'foo', false); // Disable unique push of strings and numbers. Returns ['foo', 'bar', 'baz', 'foo']
Returns random element from array.
import { random } from '@schascha/brabbelback';
const array = ['foo', 'bar', 'baz'];
console.log(random(array)); // 'foo' or 'bar' or 'baz'
Randomize array.
import { shuffle } from '@schascha/brabbelback';
const array = ['foo', 'bar'];
console.log(shuffle(array)); // ['foo', 'bar'] or ['bar', 'foo']
Returns value as an array if it's not one.
import { toArray } from '@schascha/brabbelback';
console.log(toArray('foo')); // ['foo']
console.log(toArray(['foo'])); // ['foo']
console.log(toArray()); // []
Conditional class name helper.
import { classnames } from '@schascha/brabbelback';
classnames('foo', 'bar', { baz: true }); // => 'foo bar baz'
classnames(['foo', 'bar', 'baz']); // => 'foo bar baz'
classnames({ foo: true, bar: undefined }, 'baz'); // 'foo baz'
Create a cookie.
import { setCookie } from '@schascha/brabbelback';
setCookie('name', 'value');
setCookie('name', 'value', 7); // Create a cookie that expires 7 days from now
Get cookie by name.
import { getCookie } from '@schascha/brabbelback';
getCookie('name');
Remove cookie.
import { removeCookie } from '@schascha/brabbelback';
removeCookie('name'); // Same as setCookie('name', '', 0);
Clear all cookies.
import { clearCookies } from '@schascha/brabbelback';
clearCookies();
Copy text to the clipboard.
import { copy } from '@schascha/brabbelback';
copy('foo');
Add days to a date.
import { addDays } from '@schascha/brabbelback';
console.log(addDays(new Date('2020-01-01'), 1)); // new Date('2020-01-02')
Add months to a date.
import { addMonths } from '@schascha/brabbelback';
console.log(addMonths(new Date('2020-01-01'), 1)); // new Date('2020-02-01')
Add years to a date.
import { addYears } from '@schascha/brabbelback';
console.log(addYears(new Date('2020-01-01'), 1)); // new Date('2021-01-01')
Get the difference in days between two dates.
import { diffInDays } from '@schascha/brabbelback';
console.log(diffInDays(new Date('2020-01-01'), new Date('2020-02-01'))); // 31
Get the number of days in a month.
import { daysInMonth } from '@schascha/brabbelback';
console.log(daysInMonth(new Date('2020-01-01')); // 31
Format a date as YYYY-MM-DD or a custom format.
import { formatDate } from '@schascha/brabbelback';
console.log(formatDate(new Date('2020-01-31')); // 2020-01-31
console.log(formatDate(new Date('2020-01-31'), 'DD.MM.YYYY'); // 31.01.2020
Debounce function to prevent multiple calls in a short period of time.
import { debounce } from '@schascha/brabbelback';
Throttle function to reduce the trigger rate.
import { throttled } from '@schascha/brabbelback';
Compare values.
import { compare } from '@schascha/brabbelback';
Filter an array of objects with multiple criteria.
import { filter } from '@schascha/brabbelback';
Group array items by key.
import { groupBy } from '@schascha/brabbelback';
const array = [
{ group: 'foo', name: 'Item 1' },
{ group: 'bar', name: 'Item 2' },
{ group: 'foo', name: 'Item 3' },
{ name: 'Item 4' },
];
console.log(groupBy(array, 'group'));
/*
{
foo: [
{group: 'foo', name: 'Item 1'},
{group: 'foo', name: 'Item 3'}
],
bar: [
{group: 'bar', name: 'Item 2'}
],
undefined: [
{ name: 'Item 4'}
]
}
*/
import { injectScript } from '@schascha/brabbelback';
import { gcd } from '@schascha/brabbelback';
console.log(gcd(8, 12)); // 4
import { ratio } from '@schascha/brabbelback';
console.log(ratio(1024, 768)); // 4:3
Scroll to the top of the element.
import { scrollToTop } from '@schascha/brabbelback';
const el = document.querySelector('.el');
scrollToTop(el);
// Scroll to element with sticky header
scrollToTop(el, '.header');
// Scroll to element with offset
scrollToTop(el, 100);
Truncate a string to a certain length
import { truncate } from '@schascha/brabbelback';
truncate('Hello World', 8); // Hello...
Type-checking with Object.prototype.toString() method.
import { typeOf } from '@schascha/brabbelback';
console.log(typeOf('foo')); // string
console.log(typeOf(['foo'])); // array
console.log(typeOf({ foo: 'bar' })); // object
Test if a DOM element is visible on the users viewport.
import { isVisible } from '@schascha/brabbelback';
const el = document.getElementById('name');
console.log(isVisible(el));
Please let me know: https://github.com/Schascha/brabbelback/issues
Support this project and others via PayPal. Thanks
Detailed changes for each release are documented in the release notes.
Copyright (c) 2018 Sascha Künstler