2.0.2 • Published 9 months ago

objix v2.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Objix

Objix is a delightfully convienient, high performance, zero dependency and super lightweight utility which extends the javascript standard library to sugar many common use cases for working with any objects.

The functions are all non enumerable and include copies of Object class methods and Array prototype methods applied to the values of the object as well others to delete keys, stringify, promisify, memoize, compare, split/join objects, check types, log messages and trapping/observing property assignments.

This library is highly optimised with zero copy operations where possible. The source is only 3.7kb (2.8kb minified) which allows for fast loading and easy integration without additional compilation or tree shaking. Performance in most cases is significantly faster than lodash equivalents especially when working with small objects. For example ob._map(fn) is typically over 65% faster than _.mapValues(ob, fn) and some operations such as pick can be several thousand times quicker according to simple benchmarks.

Interactive docs and demos are availble on https://objix.dev/#/docs/api.

Upgrading from 1.0

Objix 2.0 now prefixes all prototype methods with '_'. This avoids name clashes with built in methods and reduces unwanted side effects and compatibility issues inherent in the 1.x releases.

The _size() method is now renamied to _len().

Getting Started - Node

  • Install:

    > npm i -save objix
  • Require:

    require('objix')
    var o = { a: 1 }._map(v => v + 1)._log()

Getting Started - Browser

<script src="https://cdn.jsdelivr.net/gh/mattaylor/objix@main/objix.min.js"></script>

<script>
  var o = { a: 1 }._map(v => v + 1)._log()
</script>

Prototype Methods

The following methods are availble to all Objects via protoype inheritence, unless overwritten by a subclass.

_mapReturn a copy of this with all entries mapped by a function
_flatMapFlatMap a function to all entries of an this
_valuesReturn values of this
_createCreate a new Object based on this as a prototoype
_keysReturn keys of this
_entriesReturn [key,value] entry pairs of this
_isCheck type of this
_hasCheck if this includes some value
_[@@iterator]Iterate through values of this
_cleanReturn a copy of this without falsey entries
_pickCreate a copy of this with only entries with specific keys or values that that match a filter function
_findFind keys of this which match a function or value
_assignAssign new properties to this
_extendAssign default properties to this
_sameReturn new object like this with properties shared with another
_diffReturn new object like this with properties not shared with another
_deleteRemove keys from this
_someTest a function against at least one entry of this
_everyTest a function against all entries of this
_atLookup value by key path
_$Coerce this into a string with configurable formatting
_cloneClone this with configurable depths
_joinJoin objects together with this with array property values
_splitSplit this into multiple objects from array property values
_containsCheck if this contains all entries from another object to a given depth.
_eqCompare key and value identity between this and other objects to a given depth
_lenReturn number of entres in this.
_keyByRe-index values of this this using a given key path
_memoMemoize this as a function with configurable result cache expiration
_bindAssign a function as a method of this with optional memoization
_logConditionally write this to the console with an optional message
_tryCall a function against this and catch any exceptions
_trapCreate a proxy around this to intercept property assignments
_newCreate a new object from another using this as a prototype, including traps
_waitCreate a Promise which resolves this after a timeout or as determined by another function

Fluent Method Chaining

Most of these function return objects including those modifying this and so can be fluently chained together.

var o = { a: 0, b: 1, c: 2 }
  ._pick(v => v > 0)
  ._log('POSITIVE') // 2022-10-07T00:00 POSITIVE { b: 1, c: 2 }
  ._map(v => v + 1)
  ._log('INCREMENT') // 2022-10-07T00:00 INCREMENT { b: 2, c: 3 }

Function Aliases

All functions documented below are also callable with a '_' prefix to the function name. This can help ensure that the function is callable when overwritten by other object property assignments.

var o = { a: 1 }._len() == { a: 1 }._len() //true
var o = { a: 1 }._find(v => v) == { a: 1 }._find(v => v) //true

Simple Classes

Any object can act as a class from which new objects can be derived. All properties of this are inherited - including traps!!

var Person = { firstName: 'john', lastName: 'doe' }
  ._trap(v => new Date(v).getDate(), 'Invalid date', 'dob')
  ._bind('age', t => Math.floor((Date.now() - new Date(t.dob)) / 31536000000))
  ._bind('name', t => t.firstName + ' ' + t.lastName)

var p1 = Person._new({ firstName: 'jane' })
p1.name() // 'jane doe'
p1._try(
  p => (p.dob = 'foobar'),
  e => e._log()
) // Uncaught 'Invalid date [dob, foobar]'
p1.dob = '10/10/2000'
p1.age() // 22

Module Exports

All functions listed below are also available using traditional module exports, where the first argument of the function will be the object that the function is targeting as this if called via the object O.p.

const _ = require('objix')

_.len({ a: 1 }) == { a: 1 }._len() // true
_.find({ a: 1 }, v => v) == { a: 1 }._find(v => v) //true
2.0.2

9 months ago

2.0.1

9 months ago

2.0.0

9 months ago

1.14.1

2 years ago

1.14.0

2 years ago

1.14.2

2 years ago

1.15.0

2 years ago

1.16.2

2 years ago

1.16.1

2 years ago

1.16.0

2 years ago

1.13.0

2 years ago

1.12.2

2 years ago

1.12.1

2 years ago

1.12.0

2 years ago

1.11.9

2 years ago

1.10.4

3 years ago

1.10.3

3 years ago

1.10.2

3 years ago

1.8.2

3 years ago

1.6.4

3 years ago

1.4.6

3 years ago

1.8.1

3 years ago

1.4.5

3 years ago

1.8.0

3 years ago

1.6.2

3 years ago

1.4.4

3 years ago

1.4.3

3 years ago

1.6.0

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.9.9

3 years ago

1.9.8

3 years ago

1.9.7

3 years ago

1.9.6

3 years ago

1.9.5

3 years ago

1.9.3

3 years ago

1.9.2

3 years ago

1.11.0

3 years ago

1.2.16

3 years ago

1.2.15

3 years ago

1.11.4

3 years ago

1.11.3

3 years ago

1.11.2

3 years ago

1.2.18

3 years ago

1.11.1

3 years ago

1.11.8

3 years ago

1.11.7

3 years ago

1.11.6

3 years ago

1.11.5

3 years ago

1.9.1

3 years ago

1.7.3

3 years ago

1.5.5

3 years ago

1.9.0

3 years ago

1.7.2

3 years ago

1.5.4

3 years ago

1.7.1

3 years ago

1.5.3

3 years ago

1.7.0

3 years ago

1.5.2

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.8.8

3 years ago

1.8.7

3 years ago

1.6.9

3 years ago

1.8.6

3 years ago

1.6.8

3 years ago

1.6.7

3 years ago

1.4.9

3 years ago

1.8.4

3 years ago

1.6.6

3 years ago

1.4.8

3 years ago

1.8.3

3 years ago

1.6.5

3 years ago

1.4.7

3 years ago

1.10.1

3 years ago

1.10.0

3 years ago

1.2.14

3 years ago

1.2.13

3 years ago

1.2.12

3 years ago

1.2.11

3 years ago

1.2.10

3 years ago

1.2.9

3 years ago

1.2.8

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago