Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
kern committed Feb 19, 2016
0 parents commit ef55b68
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Logs
log
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules

# OSX
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
.AppleDouble
.LSOverride
Icon

# Pavlov
dist
12 changes: 12 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Copyright (c) 2015, Distributed Systems, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 changes: 42 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# ==============================================================================
# config

.PHONY: default build clean format install lint publish test

default: test

WATCH ?= false

# ==============================================================================
# phony targets

build:
./node_modules/.bin/babel src --ignore __tests__,__mocks__ --out-dir dist

clean:
@- rm -rf node_modules

format: | node_modules
./node_modules/.bin/standard --format src/**/*.js

install:
npm install

lint: | node_modules
./node_modules/.bin/standard src/**/*.js

publish: build
npm publish

test: | node_modules
@ if [ "$(WATCH)" = false ]; then \
./node_modules/.bin/jest; \
else \
./node_modules/.bin/jest --watch; \
fi

# ==============================================================================
# file targets

node_modules:
npm install
61 changes: 61 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "invar",
"version": "1.0.0",
"description": "Runtime invariant checks for better errors.",
"license": "BSD-3-Clause",
"main": "src/index.js",
"scripts": {
"test": "make test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/pavlovml/invar.git"
},
"bugs": {
"url": "https://github.com/pavlovml/invar/issues"
},
"engines": {
"node": ">= 5.0.0",
"npm": ">= 3.3.0"
},
"dependencies": {
"lodash": "^3.10.1"
},
"devDependencies": {
"babel-cli": "^6.2.0",
"babel-jest": "^6.0.1",
"babel-preset-es2015": "^6.1.18",
"babel-preset-stage-0": "^6.3.13",
"babel-eslint": "^4.1.5",
"eslint-plugin-babel": "^2.1.1",
"jest-cli": "^0.8.0",
"standard": "^5.4.1"
},
"babel": {
"presets": [
"es2015",
"stage-0"
]
},
"jest": {
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
"testPathDirs": [
"src"
]
},
"standard": {
"parser": "babel-eslint",
"ignore": [
"dist"
],
"global": [
"jest",
"describe",
"it",
"pit",
"expect",
"afterEach",
"beforeEach"
]
}
}
7 changes: 7 additions & 0 deletions src/InvarError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default class InvarError extends Error {
constructor (message, opts = {}) {
super(message)
this.name = 'Invariant Violation'
Object.assign(this, opts)
}
}
50 changes: 50 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
jest.dontMock('../InvarError')
jest.dontMock('../index')
jest.dontMock('json-stable-stringify')
const invariant = require('../index').default

describe('invariant', () => {
it('does nothing when the condition is truthy', () => {
expect(() => invariant(true, 'test')).not.toThrow()
})

it('throws an error when the condition is falsy', () => {
try {
invariant(false, 'test')
expect(false).toBeTruthy()
} catch (ex) {
expect(ex.name).toBe('Invariant Violation')
expect(ex.message).toBe('test')
}
})
})

describe('invariant.fail', () => {
it('always throws an error', () => {
try {
invariant(false, 'test')
expect(false).toBeTruthy()
} catch (ex) {
expect(ex.name).toBe('Invariant Violation')
expect(ex.message).toBe('test')
}
})
})

describe('invariant.equal', () => {
it('does nothing when the two values are equal', () => {
expect(() => invariant.equal('foo', 'foo', 'test')).not.toThrow()
})

it('throws an error when the two values are different', () => {
try {
invariant.equal('foo', 'bar', 'Lorem ipsum.')
expect(false).toBeTruthy()
} catch (ex) {
expect(ex.name).toBe('Invariant Violation')
expect(ex.message).toBe('Lorem ipsum.\n Expected: "foo"\n Actual: "bar"')
expect(ex.expected).toBe('foo')
expect(ex.actual).toBe('bar')
}
})
})
48 changes: 48 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import InvarError from './InvarError'
import _ from 'lodash'
import stringify from 'json-stable-stringify'

function makeInvarFnObj (obj) {
const fn = obj.invar

for (let k in obj) {
if (k !== 'invar') {
fn[k] = obj[k]
}
}

return fn
}

function throwError (message, opts) {
const stack =
`Invariant Violation: ${message}\n` +
(new Error()).stack
.split('\n')
.splice(3)
.join('\n')

throw new InvarError(
message,
_.defaults({ stack }, opts))
}

export default makeInvarFnObj({
invar (cond, message, opts = {}) {
if (!cond) {
throwError(message, opts)
}
},

fail (message, opts = {}) {
throwError(message, opts)
},

equal (expected, actual, message, opts = {}) {
if (!_.isEqual(expected, actual)) {
throwError(
`${message}\n Expected: ${stringify(expected)}\n Actual: ${stringify(actual)}`,
_.defaults({ expected, actual }, opts))
}
}
})

0 comments on commit ef55b68

Please sign in to comment.