my-little-lodash

2.3.0 • Public • Published

My Little Lodash

Javascript utility library.

Install

npm install my-little-lodash

Import

This package exports multiple builds. Developer has the responsibility to choose the one that best fits to her/his needs.

  1. Source

This is basically the source code itself but minified. There are no compilation and polyfills inside.

const utility = require('my-little-lodash')

A certain group of methods can be imported individually:

const string = require('my-little-lodash/dist/string')

Available groups are validators, array, dom, functions, hashing, object, security, string and type.

  1. Source with security group, for node.js

This built contains more methods related to the security which can only be used in node environment.

const utility = require('my-little-lodash/dist/node')
  1. UMD bundle

The source code bundled with browserify to generate a UMD bundle. This bundle can also be imported by html script tag. No compilation and polyfills. The variable attached to the browser's window object is MyLittleLodash.

const utility = require('my-little-lodash/dist/umd')

or

<script src="https://unpkg.com/my-little-lodash@2/dist/umd.js" crossorigin type="text/javascript"></script>
  1. Polyfilled UMD bundle

The source code compiled with babel and bundled with browserify to generate a UMD bundle. This bundle can also be imported by html script tag. The variable attached to the browser's window object is MyLittleLodash.

<script src="https://unpkg.com/my-little-lodash@2/dist/umd.polyfilled.js" crossorigin type="text/javascript"></script>

API

All methods listed below seperated by groups which they can be imported individually.

String

.case(type, str, locale)

Changes the case of the string. Available types are upper, lower, title, sentence

utility.case('upper', 'Gözel', 'tr')
// returns "GÖZEL"

utility.case('lower', 'Sandık', 'tr')
// returns "sandık"

utility.case('title', 'murat gözel', 'tr')
// returns "Murat Gözel"

utility.case('sentence', 'i am on my way.')
// returns "I am on may way"

.template(str, props)

Replaces {{name}} expressions in a string with given props[name].

utility.template('Hey {{name}}!', {name: 'Murat'})
// returns "Hey Murat!"

.sprintf(message, args)

Replaces %s expressions in a string with given arguments in order.

utility.sprintf('Hey %s', 'Murat')
// Returns "Hey Murat"

utility.sprintf('Hey %s, you have %s items in your cart.', ['Murat', 4])
// Returns "Hey Murat, you have 4 items in your cart."

.slugify(str)

Generates URL slugs. Uses limax library under the hood. Refer to the limax library for options.

utility.slugify('How y doing?', {lang: 'en'})
// returns "how-y-doing"

utility.slugify('Merhaba, nasılsınız?')
// returns "merhaba-nasilsiniz"

Validators

.isValidURL(value, options)

Returns a boolean indicating that the value is URL or not.

// available options (default)
utility.isValidURL('ftp://123.45.67.89', {
  schemes: ['http', 'https'],
  allowLocal: false,
  allowDataUrl: false
})
// returns false

Hashing

.hashcode

Generates a numeric hash from a string. It's simple and quick. May return negative.

utility.hashcode('hey lorem ipsum')
// returns -787961961

Objects

.getProp(obj, paths, defaultValue = undefined)

Returns the value of the object at specified path.

const obj = {
  name: 'Murat',
  address: {
    country: 'TR'
  }
}
utility.getProp(obj, 'name') // returns 'Murat'
utility.getProp(obj, ['address', 'country']) // returns 'TR'
utility.getProp(obj, 'nonExistingProp', 'none') // returns 'none'

.removeProp(prop, obj)

Creates a new object by removing specified prop from the obj

Arrays

.removeDuplicates(arr)

Removes duplicate items in the array.

.sortItemsBy(arr, paths, order = 'asc')

Sorts array of objects according to a certain path.

const arr = [
  {n: 'a', num: 2, props: {num: 3}},
  {n: 'b', num: 3, props: {num: 1}},
  {n: 'c', num: 1, props: {num: 2}},
]

utility.sortItemsBy(arr, 'num', 'asc')
utility.sortItemsBy(arr, ['props', 'num'], 'desc')

Functions

.waitForIt(condition, callback, interval = 300, timeout = 10000)

Runs condition function every interval miliseconds and runs callback when condition returns true. callback also runs if condition doesn't return true after timeout miliseconds.

// wait for library to be available
function checkLib() {
  return 'someLibrary' in window
}

function useLibrary() {
  window.someLibrary.someMethod.call()
}

utility.waitForIt(checkLib, useLibrary)
// interval is 0.3 and timeout is 10 seconds by default

.debounce(func, wait, options)

Shamelessly taken from lodash

.throttle(func, wait, options)

Shamelessly taken from lodash

Type Related

.getType(value)

Returns the type of the value. Possible values are object, array, promise (result of the executed function.), error (native javascript error object.), date, null, undefined, function, number, nan, regexp, string, boolean, domelement and none (if all detections fail.)

.isEmpty(value)

Returns true if the value is an empty object, empty array, empty string, 0, false or something else.

.isString(value)

.isObject(value)

.isArray(value)

.isPromise(value)

.isError(value)

.isDate(value)

.isFunction(value)

.isNull(value)

.isUndefined(value)

.isNumber(value)

.isInteger(value)

.isNan(value)

.isRegExp(value)

.isBoolean(value)

.isDOMElement(value)

.isEqual(value, otherValue)

Recursively checks the equality of two inputs.

.objectifyError(error)

Takes a native javascript error object and converts it to a javascript object.

.stringifyError(error)

Stringifies native error object.

DOM

.getViewportDimensions()

Returns the width and height of user's current viewport:

const {width, height} = utility.getViewportDimensions()

.hasWEBPSupport():Promise

Returns a promise that resolves after detected the support of client's browser for webp images.

utility.hasWEBPSupport().then(function(hasWEBPSupport) {
  // hasWEBPSupport is true or false
})

Security

.sanitizeXSS()

Filters input for XSS entries.

utility.sanitizeXSS(input)

cleanupHTML()

Cleans up all html tags.

utility.cleanupHTML(input)

Babel Polyfills Report

This module uses the following polyfills in its polyfilled builds.

  1. es.array.find
  2. es.date.to-string
  3. es.number.constructor
  4. es.regexp.constructor
  5. es.number.is-finite
  6. es.object.to-string
  7. es.regexp.exec
  8. es.regexp.to-string
  9. es.array.reduce
  10. es.object.keys
  11. es.parse-float
  12. es.array.index-of
  13. es.object.get-own-property-names
  14. es.array.filter
  15. es.array.join
  16. es.array.map
  17. es.array.sort
  18. es.object.assign
  19. es.string.match
  20. es.string.replace
  21. es.string.split
  22. es.promise

Package Sidebar

Install

npm i my-little-lodash

Weekly Downloads

1

Version

2.3.0

License

MIT

Unpacked Size

516 kB

Total Files

66

Last publish

Collaborators

  • muratgozel