Skip to content

Commit

Permalink
Merge pull request #81 from erikras/upgrade-deps
Browse files Browse the repository at this point in the history
Upgraded deps, fixed build, added code cov
  • Loading branch information
erikras authored Apr 18, 2017
2 parents 652d6ae + e0e3eac commit f2df69a
Show file tree
Hide file tree
Showing 14 changed files with 389 additions and 535 deletions.
11 changes: 1 addition & 10 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
{
"extends": "eslint-config-airbnb",
"env": {
"browser": true,
"mocha": true,
"node": true
},
"rules": {
"comma-dangle": 0, // not sure why airbnb turned this on. gross!
"indent": [2, 2, {"SwitchCase": 1}]
}
"extends": "react-app"
}
15 changes: 11 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
language: node_js

before_install:
- npm install -g npm@latest

node_js:
- "4.0"
- "4"
- "6.0"
- "6"
- "5"
- "4"
- "stable"

script:
- npm run lint
- npm test
- npm test

after_success:
- npm run test:cov
- npm run test:codecov
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Erik Rasmussen
Copyright (c) 2017 Erik Rasmussen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
30 changes: 18 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"clean": "rimraf dist lib",
"lint": "eslint src",
"prepublish": "npm run lint && npm run test && npm run clean && npm run build",
"test": "mocha --compilers js:babel-register --recursive"
"test": "mocha --compilers js:babel-register --recursive \"src/**/__tests__/*\"",
"test:cov": "cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text npm test",
"test:codecov": "cat ./coverage/lcov.info | ./node_modules/codecov.io/bin/codecov.io.js"
},
"keywords": [
"memoize",
Expand All @@ -31,21 +33,25 @@
},
"homepage": "https://github.com/erikras/lru-memoize",
"devDependencies": {
"babel-cli": "^6.23.0",
"babel-core": "^6.23.1",
"babel-eslint": "^7.1.1",
"babel-loader": "^6.3.1",
"babel-preset-es2015": "^6.22.0",
"babel-register": "^6.23.0",
"eslint": "^3.15.0",
"eslint-config-airbnb": "14.1.0",
"babel-cli": "^6.24.1",
"babel-core": "^6.24.1",
"babel-eslint": "^7.2.2",
"babel-loader": "^6.4.1",
"babel-preset-es2015": "^6.24.1",
"babel-register": "^6.24.1",
"codecov.io": "^0.1.6",
"cross-env": "^4.0.0",
"eslint": "^3.19.0",
"eslint-config-react-app": "^0.6.2",
"eslint-plugin-flowtype": "^2.30.4",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-react": "^6.9.0",
"eslint-plugin-react": "^6.10.3",
"expect": "^1.20.2",
"mocha": "^3.2.0",
"nyc": "^10.2.0",
"rifraf": "^2.0.3",
"rimraf": "^2.5.4",
"webpack": "^2.2.1"
"rimraf": "^2.6.1",
"webpack": "^2.4.1"
}
}
110 changes: 110 additions & 0 deletions src/__tests__/deepEquals.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import expect from 'expect'
import deepEquals from '../deepEquals'

const tripleEquals = deepEquals((valueA, valueB) => valueA === valueB, true)

describe('deepEquals', () => {
it('should return true if argument fields are equal', () => {
expect(tripleEquals(3, 3)).toBe(true)

expect(tripleEquals('dog', 'dog')).toBe(true)

expect(
tripleEquals({ a: 1, b: 2, c: undefined }, { a: 1, b: 2, c: undefined })
).toBe(true)

expect(tripleEquals({ a: 1, b: 2, c: 3 }, { a: 1, b: 2, c: 3 })).toBe(true)

const obj = {}
expect(tripleEquals({ a: 1, b: 2, c: obj }, { a: 1, b: 2, c: obj })).toBe(
true
)

expect(tripleEquals(null, null)).toBe(true)
})

it('should return false if arguments are number and string', () => {
expect(tripleEquals(2, '2')).toBe(false)
})

it('should return false if arguments are string and number', () => {
expect(tripleEquals('2', 2)).toBe(false)
})

it('should return false if arguments are number and object', () => {
expect(tripleEquals(4, {})).toBe(false)
})

it('should return false if arguments are object and number', () => {
expect(tripleEquals({}, 4)).toBe(false)
})

it('should return false if arguments are number and array', () => {
expect(tripleEquals(4, [])).toBe(false)
})

it('should return false if arguments are array and number', () => {
expect(tripleEquals([], 4)).toBe(false)
})

it('should return false if arguments are string and object', () => {
expect(tripleEquals('cat', {})).toBe(false)
})

it('should return false if arguments are object and string', () => {
expect(tripleEquals({}, 'cat')).toBe(false)
})

it('should return false if arguments are string and array', () => {
expect(tripleEquals('cat', ['c', 'a', 't'])).toBe(false)
})

it('should return false if arguments are array and string', () => {
expect(tripleEquals(['c', 'a', 't'], 'cat')).toBe(false)
})

it('should return false if arguments are array and object', () => {
expect(tripleEquals([], {})).toBe(false)
})

it('should return false if arguments are object and array', () => {
expect(tripleEquals({}, [])).toBe(false)
})

it('should return false if arguments are object and null', () => {
expect(tripleEquals({ a: 1 }, null)).toBe(false)
})

it('should return false if arguments are null and object', () => {
expect(tripleEquals(null, { a: 1 })).toBe(false)
})

it('should return false if first argument has too many keys', () => {
expect(tripleEquals({ a: 1, b: 2, c: 3 }, { a: 1, b: 2 })).toBe(false)
})

it('should return false if second argument has too many keys', () => {
expect(tripleEquals({ a: 1, b: 2 }, { a: 1, b: 2, c: 3 })).toBe(false)
})

it('should return false if arguments have different keys', () => {
expect(
tripleEquals({ a: 1, b: 2, c: undefined }, { a: 1, bb: 2, c: undefined })
).toBe(false)
})

it('should return false if first array argument has too many items', () => {
expect(tripleEquals([1, 2, 3, 4], [1, 2, 3])).toBe(false)
})

it('should return false if second array argument has too many items', () => {
expect(tripleEquals([1, 2, 3], [1, 2, 3, 4])).toBe(false)
})

it('should work with objects inside arrays', () => {
expect(tripleEquals(
[ { val: 4 }, { val: 2 }, { val: 3 } ],
[ { val: 1 }, { val: 2 }, { val: 3 } ]
)).toBe(false)
})
})
180 changes: 180 additions & 0 deletions src/__tests__/memoize.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import expect from 'expect'
import memoize from '../memoize'

describe('memoize', () => {
describe('basic', () => {
it('single', () => {
let callCount = 0
let multiply = (x, y, z) => {
callCount += 1
return x * y * z
}
multiply = memoize()(multiply)

expect(multiply(1, 2, 3)).toBe(6)

expect(multiply(1, 2, 3)).toBe(6)

expect(callCount).toBe(1)

expect(multiply(4, 5, 6)).toBe(120)

expect(callCount).toBe(2)

expect(multiply(1, 2, 3)).toBe(6)

expect(callCount).toBe(3)
})

it('multiple', () => {
let callCount = 0
let multiply = (x, y, z) => {
callCount += 1
return x * y * z
}
multiply = memoize(2)(multiply)

expect(multiply(1, 2, 3)).toBe(6)

expect(multiply(1, 2, 3)).toBe(6)

expect(callCount).toBe(1)

expect(multiply(4, 5, 6)).toBe(120)

expect(callCount).toBe(2)

expect(multiply(1, 2, 3)).toBe(6)

expect(callCount).toBe(2)

expect(multiply(4, 5, 6)).toBe(120)

expect(callCount).toBe(2)
})
})

describe('shallow', () => {
it('array', () => {
let callCount = 0
let multiply = (x, y, z) => {
callCount += 1
return x.concat(y, z).reduce((t, n) => t * n)
}
multiply = memoize()(multiply)

const x = [1, 2, 3]
const y = [4, 5, 6]
const z = [7, 8, 9]

const x2 = [1, 2, 3]

expect(multiply(x, y, z)).toBe(362880)

expect(multiply(x2, y, z)).toBe(362880)

expect(callCount).toBe(2)
})

it('object', () => {
let callCount = 0
let multiply = (x, y, z) => {
callCount += 1
return x.val * y.val * z.val
}
multiply = memoize(2)(multiply)

const x = { val: 1 }
const y = { val: 2 }
const z = { val: 3 }

const x2 = { val: 1 }

expect(multiply(x, y, z)).toBe(6)

expect(multiply(x2, y, z)).toBe(6)

expect(callCount).toBe(2)
})
})

describe('deep', () => {
it('array', () => {
let callCount = 0
let multiply = (x, y, z) => {
callCount += 1
return x.concat(y, z).reduce((t, n) => t * n)
}
multiply = memoize(true)(multiply)

const x = [1, 2, 3]
const y = [4, 5, 6]
const z = [7, 8, 9]

const x2 = [1, 2, 3]
const x3 = [3, 2, 1]

expect(multiply(x, y, z)).toBe(362880)

expect(multiply(x2, y, z)).toBe(362880)

expect(callCount).toBe(1)

expect(multiply(x3, y, z)).toBe(362880)

expect(callCount).toBe(2)
})

it('object', () => {
let callCount = 0
let multiply = (x, y, z) => {
callCount += 1
return x.val * y.val * z.val
}
multiply = memoize(true)(multiply)

const x = { val: 1 }
const y = { val: 2 }
const z = { val: 3 }

const x2 = { val: 1 }
const x3 = { val: 4 }

expect(multiply(x, y, z)).toBe(6)

expect(multiply(x2, y, z)).toBe(6)

expect(callCount).toBe(1)

expect(multiply(x3, y, z)).toBe(24)

expect(callCount).toBe(2)
})

it('object nested', () => {
let callCount = 0
let multiply = (x, y, z) => {
callCount += 1
return x.inner.val * y.inner.val * z.inner.val
}
multiply = memoize(true)(multiply)

const x = { inner: { val: 1 } }
const y = { inner: { val: 2 } }
const z = { inner: { val: 3 } }

const x2 = { inner: { val: 1 } }
const x3 = { inner: { val: 4 } }

expect(multiply(x, y, z)).toBe(6)

expect(multiply(x2, y, z)).toBe(6)

expect(callCount).toBe(1)

expect(multiply(x3, y, z)).toBe(24)

expect(callCount).toBe(2)
})
})
})
Loading

0 comments on commit f2df69a

Please sign in to comment.