New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

lutils

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lutils

A small set of browser-friendly utilities

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.8K
decreased by-7.13%
Maintainers
1
Weekly downloads
 
Created
Source

LUtils

A few important utilities which are implimented differently to libraries like underscore. Each module is as small as it needs to be, which is ideal for use in the browser.

Include all of them at once or be selective.

{ typeOf, merge } = require 'lance-utils'
typeOf = require 'lance-utils/typeOf'

Clone

clone (object, depth = 8)

Clones an object.

Tries to JSON clone it first, then clone.merge it if that fails, while also merging in clone.findRegExp to preserve any RegExp.

clone.merge (object, depth = 10)

Deep clones by recursively iterating over each object/array key, building a new one with each value.

clone.json (object)

Clones via JSON.stringify then JSON.parse

clone.findRegExp (object)

Recursively finds all RegExp in an object by creating a deep skeleton object containing only the RegExp values. Useful when you wish to preserve RegExp (which can't be done with JSON cloning).

Merge

merge (object1, object2, depth = 8, iterators = [ 'object' ])

Merge the second object into the first recursively until depth is reached for each property, replacing object1's values with those in object2.

iterators is an array of types that, when matched on a value, will be iterated over and merged in. This means you can merge a function's properties or an array's properties recursively, thus preserving pointer references to the first object's instance.

fn1 = -> return 'fn1' + fn1.prop.b
fn1.prop = { a: 1 }

fn2 = -> return 'fn2' + fn2.prop.a
fn2.prop = { b: 2 }

obj1 = { a: { b: { fn: fn1 } } }
obj2 = { a: { b: { fn: fn2 } } }

merge obj1, obj2
# >> { a: { b: { fn: [Function] } } }

obj1.a.b.fn.prop
# >> { a: 1, b: 2 }

obj1.a.b.fn()
# 'fn1!2'
merge.white (object1, object2, depth = 8, iterators = [ 'object' ])

Whitelisted merge. Merges properties into object1 from object only if the property exists in object1

merge.black (object1, object2, depth = 8, iterators = [ 'object' ])

Blacklisted merge. Merges properties into object1 from object only if the property doesnt exist in object1

merge.iterators {Array}

Default iterator types array, [ 'object' ]

merge.depth {Number}

Default depth, 8

TypeOf

typeOf (value)

Returns the primitive type of a value as a lowercase string, very reliable. To be used in combination with instanceof and object.constructor.name when necessary.

typeOf 'a string' # >> 'object'
typeOf { an: { object: null } } # >> 'object'
typeOf null # >> 'null'
typeOf 0 # >> 'number'

Also has helper properties which return a boolean.

typeOf.Type (value)
typeOf.RegExp 'not regex' # false
typeOf.Object null # false
typeOf.Array [] # true

###
	Avaliable properties (lowercase as well):
		typeOf.Undefined
		typeOf.Boolean
		typeOf.String
		typeOf.Function
		typeOf.Array'
		typeOf.Object
		typeOf.Null
		typeOf.Number
		typeOf.Date
		typeOf.RegExp
		typeOf.NaN
		typeOf.Symbol
		typeOf.Buffer
###

Debounce

debounce (optionsObject, callback)
# Default options
options = {
	wait: 0 # Time in ms to accumulate callbacks before finishing
	maxWait: Infinity # When reached will call the callback no matter what
	immediate: false # Whether to immediately call the callback the first time (Currently broken)
}

options.wait = 1000

fn = debounce options, ->
	console.log 'woo!'

setInterval fn, 100
# In 10 seconds, 'woo!' will be logged 10 times instead of 100 times

FAQs

Package last updated on 20 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc