Skip to content

Commit

Permalink
feat(exists): The exists function is useful to find out if a chained …
Browse files Browse the repository at this point in the history
…object exists.
  • Loading branch information
jusx committed Mar 6, 2019
1 parent b1c5b17 commit 5837ff7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
58 changes: 43 additions & 15 deletions __tests__/object-dot.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
const odjectd = require('object-dot')
const objectd = require('object-dot')

describe('set', () => {
test('deep nested', () => {
let object = {}
odjectd.set({ object, path: 'a.b.c.d.e.f.g.h.i.j.k', value: 'my value' })
objectd.set({ object, path: 'a.b.c.d.e.f.g.h.i.j.k', value: 'my value' })
expect(object.a.b.c.d.e.f.g.h.i.j.k).toBe('my value')
})

test('medium nested', () => {
let object = {}
odjectd.set({ object, path: 'a.b.c.d', value: 'my value' })
objectd.set({ object, path: 'a.b.c.d', value: 'my value' })
expect(object.a.b.c.d).toBe('my value')
})

test('shallow nested', () => {
let object = {}
odjectd.set({ object, path: 'a', value: 'my value' })
objectd.set({ object, path: 'a', value: 'my value' })
expect(object.a).toBe('my value')
})

test('array over dot notation', () => {
let object = {}
odjectd.set({ object, path: ['a', 'b', 'c'], value: 'my value' })
objectd.set({ object, path: ['a', 'b', 'c'], value: 'my value' })
expect(object).toEqual({ a: { b: { c: 'my value' } } })
})

test('some property in the chain exists', () => {
let object = { a: { exist: true } }
odjectd.set({ object, path: 'a.b', value: 'foo' })
objectd.set({ object, path: 'a.b', value: 'foo' })
expect(object).toEqual({ a: { exist: true, b: 'foo' } })
})

test('with arguments instead of destructing object', () => {
let object = { a: { exist: true } }
odjectd.set(object, 'a.b', 'foo')
objectd.set(object, 'a.b', 'foo')
expect(object.a.b).toBe('foo')
})
})
Expand All @@ -42,54 +42,82 @@ describe('get', () => {
test('exists', () => {
let object = { a: { b: { c: 'd' } } }
expect(
odjectd.get({ object, path: 'a.b.c' })
objectd.get({ object, path: 'a.b.c' })
).toBe('d')
})

test('exist in the middle ', () => {
let object = { a: { b: { c: 'd' } } }
expect(
odjectd.get({ object, path: 'a.b' })
objectd.get({ object, path: 'a.b' })
).toEqual({ c: 'd' })
})

test('does not exist', () => {
let object = {}
expect(
odjectd.get({ object, path: 'a.b.c' })
objectd.get({ object, path: 'a.b.c' })
).toBeUndefined()
})

test('default', () => {
let object = {}
expect(
odjectd.get({ object, path: 'a.b.c', value: 'foo' })
objectd.get({ object, path: 'a.b.c', value: 'foo' })
).toBe('foo')
})

test('array over dot notation', () => {
let object = { a: { b: { c: 'd' } } }
expect(
odjectd.get({ object, path: ['a', 'b', 'c'] })
objectd.get({ object, path: ['a', 'b', 'c'] })
).toEqual('d')

object.c = undefined
expect(
odjectd.get({ object, path: ['a', 'b', 'c'], value: 'default' })
objectd.get({ object, path: ['a', 'b', 'c'], value: 'default' })
).toEqual('default')
})

test('with arguments instead of destructing object', () => {
let object = { a: { b: 'foo' } }
expect(
odjectd.get(object, 'a.b')
objectd.get(object, 'a.b')
).toBe('foo')
})

test('null values are acceptable', () => {
let object = { a: { b: null } }
expect(
odjectd.get(object, 'a.b')
objectd.get(object, 'a.b')
).toBeNull()
})
})

describe('exists', () => {
const object = { foo: { bar: { a: { b: 'foo' } } } }

test('is true', () => {
expect(
objectd.exists(object, 'foo.bar.a')
).toBe(true)
})

test('is false', () => {
expect(
objectd.exists(object, 'foo.bar.d')
).toBe(false)
})

test('is true and deep edge of the chain', () => {
expect(
objectd.exists(object, 'foo.bar.a.b')
).toBe(true)
})

test('is false and deep edge of the chain', () => {
expect(
objectd.exists(object, 'foo.bar.a.e')
).toBe(false)
})
})
14 changes: 13 additions & 1 deletion lib/object-dot.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,16 @@ function get ({ object, path, value }) {
return (value === undefined) ? object : value
}

module.exports = { set, get }
function exists ({ object, path }) {
if (arguments.length > 1) [ object, path ] = arguments

let properties = (Array.isArray(path)) ? path : path.split('.')
while (properties.length) {
object = object[properties.shift()]
if (object === undefined) return false
}

return true
}

module.exports = { set, get, exists }

0 comments on commit 5837ff7

Please sign in to comment.