Skip to content

Commit

Permalink
fixes the array index unset issue (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
bali182 authored and sdgluck committed Mar 30, 2017
1 parent 990e5c2 commit d47bda0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ function mewt(target) {
return mewt(newObj)
},
$unset (prop) {
if (isA && Number.isInteger(prop) && prop >= 0) {
return mewt(target.slice(0, prop).concat(target.slice(prop + 1)))
}
const newObj = clone(target)
delete newObj[prop]
return mewt(newObj)
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 23 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ describe('mewt', () => {
it('should throw when null given', () => {
expect(() => mewt(null)).toThrowError(/accepts array or object/)
})

it('should throw when string given', () => {
expect(() => mewt('foo')).toThrowError(/accepts array or object/)
})

it('should throw when number given', () => {
expect(() => mewt(123)).toThrowError(/accepts array or object/)
})

it('should throw when boolean given', () => {
expect(() => mewt(true)).toThrowError(/accepts array or object/)
expect(() => mewt(false)).toThrowError(/accepts array or object/)
})

it('should throw when function given', () => {
expect(() => mewt(() => { })).toThrowError(/accepts array or object/)
})

it('should throw when Symbol given', () => {
expect(() => mewt(Symbol('foo'))).toThrowError(/accepts array or object/)
})
Expand Down Expand Up @@ -76,14 +76,23 @@ describe('mewt', () => {
assertMewt(n)
})

xit('should properly $unset at index', () => {
it('should properly $unset at index', () => {
const a = mewt([''])
const n = a.$unset(0)
expect(n).toEqual([])
expect(a).not.toBe(n)
assertMewt(n)
})

it('should properly $unset negative indexes and string keys', () => {
const a = mewt([1, 2, 3])
const n = a.$set('foo', 'bar')
.$set(-1, 42)
.$unset(-1)
.$unset('foo')
expect(n).toEqual([1, 2, 3])
})

it('should copyWithin without mutation', () => {
const a = mewt([1, 2])
const n = a.copyWithin(0, 2)
Expand Down Expand Up @@ -224,9 +233,9 @@ describe('mewt', () => {
it('should throw on setPrototypeOf', () => {
const o = mewt({})
expect(() => {
Object.setPrototypeOf(o, {bar: 'baz'})
Object.setPrototypeOf(o, { bar: 'baz' })
}).toThrowError(/immutable/)
})
})

it('should have $set & $unset', () => {
const o = mewt({})
Expand All @@ -253,5 +262,11 @@ describe('mewt', () => {
expect(n.album).toBeUndefined()
expect(o).not.toBe(n)
})

it('should properly $unset numeric indexes', () => {
const o = mewt({ foo: 'bar', 0: 'foo', 1: 'bar' })
expect(o.$unset(0)).toEqual({ foo: 'bar', 1: 'bar' })
expect(o.$unset(1)).toEqual({ foo: 'bar', 0: 'foo' })
})
})
})

0 comments on commit d47bda0

Please sign in to comment.