Skip to content

Commit

Permalink
Format code as part of linting
Browse files Browse the repository at this point in the history
  • Loading branch information
prayerslayer committed Aug 14, 2017
1 parent 2971f15 commit 661a21f
Show file tree
Hide file tree
Showing 25 changed files with 643 additions and 509 deletions.
6 changes: 4 additions & 2 deletions .eslintrc.json
@@ -1,12 +1,14 @@
{
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error"
}
}
6 changes: 6 additions & 0 deletions .eslintrc.test.json
@@ -0,0 +1,6 @@
{
"env": {
"mocha": true
},
"extends": ".eslintrc.json"
}
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -9,5 +9,6 @@ cache:
install:
- npm install
script:
- npm run lint
- npm run coveralls
- npm run build
26 changes: 13 additions & 13 deletions index.js
@@ -1,10 +1,10 @@
import * as util from './lib/util';
import * as specs from './lib/spec';
import { catImpl as cat, altImpl as alt } from './lib/regex';
import * as predicates from './lib/predicates';
import * as symbols from './lib/symbols';
import getIn from 'lodash.get';
import flatten from 'lodash.flattendeep';
import * as util from "./lib/util";
import * as specs from "./lib/spec";
import { catImpl as cat, altImpl as alt } from "./lib/regex";
import * as predicates from "./lib/predicates";
import * as symbols from "./lib/symbols";
import getIn from "lodash.get";
import flatten from "lodash.flattendeep";

const specsAndPreds = Object.assign(
{
Expand All @@ -16,7 +16,7 @@ const specsAndPreds = Object.assign(
);

export { specsAndPreds as spec, symbols as symbol };
export { valid, conform } from './lib/util';
export { valid, conform } from "./lib/util";

export function explainData(spec, value) {
const problems = flatten(util.explain(spec, [], [], value)).filter(x => !!x);
Expand All @@ -28,23 +28,23 @@ export function explainData(spec, value) {

function problemStr(problem, value) {
return `${problem.via.join(
''
""
)}: ${problem.predicateName} failed for ${getIn(
value,
problem.path
)} at [${problem.path.join(', ')}].`;
)} at [${problem.path.join(", ")}].`;
}

export function explain(spec, value) {
explainData(spec, value).forEach(problem =>
console.log(problemStr(problem, value)) // eslint-disable-line
explainData(spec, value).forEach(
problem => console.log(problemStr(problem, value)) // eslint-disable-line
);
}

export function explainStr(spec, value) {
return explainData(spec, value)
.map(problem => problemStr(problem, value))
.join('\n');
.join("\n");
}

export function assert(spec, value) {
Expand Down
16 changes: 13 additions & 3 deletions lib/regex.js
@@ -1,6 +1,14 @@
// http://www.ccs.neu.edu/home/turon/re-deriv.pdf

import { dt, symbolToString, zip, valid, explain, getName, undefinedPredicateWarning } from "./util";
import {
dt,
symbolToString,
zip,
valid,
explain,
getName,
undefinedPredicateWarning
} from "./util";
import { invalid } from "./symbols";
import * as p from "./predicates";
// ops
Expand Down Expand Up @@ -164,11 +172,13 @@ export function regexExplain(regex, path, via, value) {
[...path, i],
[...via, getName(regex), regex.ks[i]],
value[i]
));
)
);
case alt:
// if value does not match alt, ALL alternatives have to yield a problem
return regex.ps.map((p, i) =>
explain(p, [...path, i], [...via, getName(regex), regex.ks[i]], value));
explain(p, [...path, i], [...via, getName(regex), regex.ks[i]], value)
);
case acc:
return null;
}
Expand Down
6 changes: 4 additions & 2 deletions lib/spec/enum.js
Expand Up @@ -12,8 +12,10 @@ export class Enum extends Spec {
}

toString() {
return this.name ||
`Enum(${this.options.values.map(p => getName(p)).join(", ")})`;
return (
this.name ||
`Enum(${this.options.values.map(p => getName(p)).join(", ")})`
);
}

explain(path, via, value) {
Expand Down
45 changes: 25 additions & 20 deletions lib/spec/keys.js
@@ -1,29 +1,31 @@
import Spec from './spec'
import { getName } from '../util'
import * as p from '../predicates'
import { invalid } from '../symbols'
import Spec from "./spec";
import { getName } from "../util";
import * as p from "../predicates";
import { invalid } from "../symbols";

export class Keys extends Spec {
conform(value) {
if (p.object(value)) {
for (const key of this.options.requiredKeys) {
if (!value.hasOwnProperty(key)) {
return invalid
return invalid;
}
}
return value
return value;
}
return invalid
return invalid;
}

explain(path, via, value) {
if (!p.object(value)) {
return [{
path,
via,
value,
predicate: p.object
}]
return [
{
path,
via,
value,
predicate: p.object
}
];
}
return this.options.requiredKeys.map(key => {
if (!value.hasOwnProperty(key)) {
Expand All @@ -32,16 +34,19 @@ export class Keys extends Spec {
via: [...via, getName(this)],
value,
predicate: function hasKey(value) {
return value.hasOwnProperty(key)
return value.hasOwnProperty(key);
}
}
};
}
return null
})
return null;
});
}

toString() {
return this.name || `Keys(${this.options.requiredKeys.map(k => getName(k)).join(", ")})`
return (
this.name ||
`Keys(${this.options.requiredKeys.map(k => getName(k)).join(", ")})`
);
}
}

Expand All @@ -50,9 +55,9 @@ export default function keys(name, ...requiredKeys) {
throw new Error(`Name ${name} must be a string.`);
}
if (requiredKeys.length === 0) {
throw new Error(`Cannot use Keys spec without keys.`)
throw new Error(`Cannot use Keys spec without keys.`);
}
return new Keys(name, {
requiredKeys
})
});
}
6 changes: 4 additions & 2 deletions lib/spec/map.js
Expand Up @@ -27,7 +27,8 @@ export class Map extends Spec {
const ret = {};
for (const key of getAllKeys(value)) {
const v = value[key];
const spec = this.options.requiredSpecs[key] ||
const spec =
this.options.requiredSpecs[key] ||
this.options.optionalSpecs[key] ||
null;
if (spec !== null) {
Expand Down Expand Up @@ -58,7 +59,8 @@ export class Map extends Spec {
}
return getAllKeys(value).map(key => {
const v = value[key];
const hasSpec = this.options.requiredSpecs[key] ||
const hasSpec =
this.options.requiredSpecs[key] ||
this.options.optionalSpecs[key] ||
null;
if (hasSpec) {
Expand Down
6 changes: 4 additions & 2 deletions lib/spec/or.js
Expand Up @@ -23,8 +23,10 @@ export class Or extends Spec {
}

toString() {
return this.name ||
`Or(${this.options.alternatives.map(([s]) => getName(s)).join(", ")})`;
return (
this.name ||
`Or(${this.options.alternatives.map(([s]) => getName(s)).join(", ")})`
);
}

explain(path, via, value) {
Expand Down
12 changes: 6 additions & 6 deletions lib/spec/spec.js
@@ -1,20 +1,20 @@
export default class Spec {
constructor(name = '', options) {
constructor(name = "", options) {
// options is data necessary to check values for conformity
this.options = options
this.options = options;
// name is for better display when printing explain() result
this.name = name
this.name = name;
}

conform(value) {
throw new Error(`Implement in subclass: conform(${value})`)
throw new Error(`Implement in subclass: conform(${value})`);
}

unform(conformed) {
throw new Error(`Implement in subclass: unform(${conformed})`)
throw new Error(`Implement in subclass: unform(${conformed})`);
}

explain(value) {
throw new Error(`Implement in subclass: explain(${value})`)
throw new Error(`Implement in subclass: explain(${value})`);
}
}
6 changes: 4 additions & 2 deletions lib/spec/tuple.js
Expand Up @@ -27,8 +27,10 @@ export class Tuple extends Spec {
}

toString() {
return this.name ||
`Tuple(${this.options.specs.map(p => getName(p)).join(", ")})`;
return (
this.name ||
`Tuple(${this.options.specs.map(p => getName(p)).join(", ")})`
);
}

explain(path, via, value) {
Expand Down
10 changes: 5 additions & 5 deletions lib/symbols.js
@@ -1,8 +1,8 @@
// returned from conform() if value is invalid according to spec
export const invalid = Symbol("invalid")
export const invalid = Symbol("invalid");
// used for map spec, denotes optional keys
export const optional = Symbol("optional")
export const optional = Symbol("optional");
// used for collection spec
export const count = Symbol("count")
export const minCount = Symbol("minCount")
export const maxCount = Symbol("maxCount")
export const count = Symbol("count");
export const minCount = Symbol("minCount");
export const maxCount = Symbol("maxCount");
12 changes: 8 additions & 4 deletions lib/util.js
Expand Up @@ -15,14 +15,16 @@ export function symbolToString(sym) {
function checkNilableAndThrow(specName, predName, pred) {
if (p.nil(pred)) {
throw new Error(
`Predicate${predName ? " " + predName : ""} of spec ${specName} is null or undefined, probably that's not your intention.`
`Predicate${predName
? " " + predName
: ""} of spec ${specName} is null or undefined, probably that's not your intention.`
);
}
}

export function undefinedPredicateWarning(spec, predicates) {
const isIterable = typeof predicates === "object" ||
Array.isArray(predicates);
const isIterable =
typeof predicates === "object" || Array.isArray(predicates);
if (predicates != null && !isIterable) {
return;
}
Expand Down Expand Up @@ -131,7 +133,9 @@ export function dt(predicate, value, returnBoolean = false) {
return predicate(value) ? value : invalid;
}
throw new Error(
`${getName(predicate)} is a ${typeof predicate}, not a function. Expected predicate`
`${getName(
predicate
)} is a ${typeof predicate}, not a function. Expected predicate`
);
}
return value;
Expand Down
6 changes: 5 additions & 1 deletion package.json
Expand Up @@ -5,7 +5,9 @@
"main": "dist/js.spec.js",
"scripts": {
"build": "webpack --config webpack.config.js",
"lint": "eslint index.js lib/**/*.js",
"lint": "npm run lint-src && npm run lint-test",
"lint-src": "eslint 'index.js' 'lib/**/*.js'",
"lint-test": "eslint --config .eslintrc.test.json 'test/**/*.js'",
"prepare": "npm run build",
"test": "mocha --recursive --compilers js:babel-register test",
"test:watch": "mocha --recursive --reporter=min --watch --compilers js:babel-register test",
Expand Down Expand Up @@ -35,8 +37,10 @@
"chai": "^3.5.0",
"coveralls": "^2.11.14",
"eslint": "^4.4.1",
"eslint-plugin-prettier": "^2.1.2",
"mocha": "^3.1.2",
"nodemon": "^1.11.0",
"prettier": "^1.5.3",
"pretty-format": "^18.0.0",
"uglify-js": "^3.0.27",
"uglifyjs-webpack-plugin": "1.0.0-beta.2",
Expand Down
18 changes: 9 additions & 9 deletions test/index.test.js
@@ -1,15 +1,15 @@
import {expect} from 'chai'
import {assert} from '../index'
import {number} from '../lib/predicates'
import { expect } from "chai";
import { assert } from "../index";
import { number } from "../lib/predicates";

describe("index", () => {
describe("assert", () => {
it("does not throw an Error when valid", () => {
expect(() => assert(number, 1)).to.not.throw(Error)
})
expect(() => assert(number, 1)).to.not.throw(Error);
});

it("throws an Error when invalid", () => {
expect(() => assert(number, "string")).to.throw(Error, "isNumber")
})
})
})
expect(() => assert(number, "string")).to.throw(Error, "isNumber");
});
});
});

0 comments on commit 661a21f

Please sign in to comment.