Skip to content

Commit

Permalink
add prettier as plugin eslint for formating
Browse files Browse the repository at this point in the history
  • Loading branch information
Eries Trisnadi committed Dec 23, 2017
1 parent 8bc800d commit 8cbe939
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 83 deletions.
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"singleQuote": true,
"trailingComma": "es5",
"bracketSpacing": true,
"printWidth": 80,
"tabWidth": 2,
"semi": true
}
26 changes: 14 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
"dependencies": {
"immutable": "~4.0.0-rc.9",
"path": "^0.12.7",
"stemmer": "^1.0.2",
"superstruct": "^0.4.5",
"uuid": "^3.1.0"
"superstruct": "^0.4.5"
},
"devDependencies": {
"babel-cli": "^6.26.0",
Expand All @@ -39,11 +37,14 @@
"coveralls": "^3.0.0",
"eslint": "^4.12.0",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-prettier": "^2.4.0",
"mocha": "^4.0.1",
"mustache": "^2.3.0",
"npm-run-all": "^4.1.2",
"nyc": "^11.3.0",
"prettier": "1.9.2",
"rollup": "^0.52.0",
"rollup-plugin-babel": "^3.0.2",
"rollup-plugin-buble": "^0.18.0",
Expand All @@ -58,23 +59,24 @@
"uglify-save-license": "^0.4.1"
},
"scripts": {
"lint": "eslint src test tools",
"test": "run-s -l lint \"test:mocha {@}\" test:cover --",
"clean:dist": "run-s \"rm -rf dist\"",
"clean:docs": "run-s \"rm -rf docs\" \"mkdir docs\"",
"test:mocha": "mocha --require babel-register",
"test:cover": "nyc --require babel-register mocha --timeout=3000",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"build": "run-s build:*",
"build": "run-s format build:*",
"build:dist": "run-s clean:dist bundle",
"build:docs": "run-s clean:docs mustache",
"bundle": "run-s bundle:*",
"bundle:cjs": "run-s \"rollup -c tools/build-cjs.js\"",
"bundle:es": "run-s \"rollup -c tools/build-es.js\"",
"format": "run-s \"lint --fix\"",
"clean:dist": "run-s \"rm -rf dist\"",
"clean:docs": "run-s \"rm -rf docs\" \"mkdir docs\"",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"lint": "eslint src test tools",
"mkdir": "shx mkdir",
"mustache": "node tools/build-docs && shx cp -r resources/css docs && mustache -p resources/partials/menu.mustache -p resources/partials/content.mustache resources/docs.json resources/docs.mustache > docs/index.html",
"prepublish": "run-s build",
"rm": "shx rm",
"rollup": "rollup"
"rollup": "rollup",
"test": "run-s -l lint \"test:mocha {@}\" test:cover --",
"test:mocha": "mocha --require babel-register",
"test:cover": "nyc --require babel-register mocha --timeout=3000"
}
}
4 changes: 2 additions & 2 deletions resources/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"pages": [
{
"title": "Add",
"content": "<pre><code class=\"javascript\">\r\n/**\r\n * Append new item to elements\r\n * The `data` is invoked with the {Object} value.\r\n *\r\n * @since 0.1.0\r\n * @param {Object} data Value of an item to add.\r\n * @returns {*} Returns the new of current array, else throw an `error`.\r\n * @example\r\n *\r\n * .jaysn file\r\n * {\r\n * \"users\": [\r\n * { \"user\": \"barney\", \"age\": 36, \"active\": true },\r\n * { \"user\": \"fred\", \"age\": 40, \"active\": false }\r\n * ]\r\n * }\r\n *\r\n */\r\n\r\n new Jaysn('users', {\r\n user: 'string',\r\n age: 'number',\r\n active: 'boolean'\r\n }).add({ user: \"pebbles\", age: 1, active: true })\r\n\r\n/**\r\n * // => objects for ['barney','fred','pebbles']\r\n */\r\n</code></pre>"
"content": "<pre><code class=\"javascript\">\n/**\n * Append new item to elements\n * The `data` is invoked with the {Object} value.\n *\n * @since 0.1.0\n * @param {Object} data Value of an item to add.\n * @returns {*} Returns the new of current array, else throw an `error`.\n * @example\n *\n * .jaysn file\n * {\n * \"users\": [\n * { \"user\": \"barney\", \"age\": 36, \"active\": true },\n * { \"user\": \"fred\", \"age\": 40, \"active\": false }\n * ]\n * }\n *\n */\n\n new Jaysn('users', {\n user: 'string',\n age: 'number',\n active: 'boolean'\n }).add({ user: \"pebbles\", age: 1, active: true })\n\n/**\n * // => objects for ['barney','fred','pebbles']\n */\n</code></pre>"
},
{
"title": "Update",
"content": "<pre><code class=\"javascript\">\r\n/**\r\n * Iterates over elements of, update an item and returning the updated item, else undefined\r\n * `predicate` returns truthy for. The `predicate` is invoked with a callback function\r\n * and `data` is invoked with the {Object} value.\r\n *\r\n * @since 0.1.0\r\n * @param {Object} data Value of an item to update.\r\n * @param {Function} predicate The callback function.\r\n * @returns {*} Returns the updated item, else undefined.\r\n * @example\r\n *\r\n * .jaysn file\r\n * {\r\n * \"users\": [\r\n * { \"user\": \"barney\", \"age\": 36, \"active\": true },\r\n * { \"user\": \"fred\", \"age\": 40, \"active\": false },\r\n * { \"user\": \"pebbles\", \"age\": 1, \"active\": true }\r\n * ]\r\n * }\r\n *\r\n */\r\n\r\n new Jaysn('users', {\r\n user: 'string',\r\n age: 'number',\r\n active: 'boolean'\r\n }).update({\r\n user: 'bonbon'\r\n }, o => o.get('age') === 36)\r\n\r\n/**\r\n * // => object for 'bonbon'\r\n */\r\n</code></pre>"
"content": "<pre><code class=\"javascript\">\n/**\n * Iterates over elements of, update an item and returning the updated item, else undefined\n * `predicate` returns truthy for. The `predicate` is invoked with a callback function\n * and `data` is invoked with the {Object} value.\n *\n * @since 0.1.0\n * @param {Object} data Value of an item to update.\n * @param {Function} predicate The callback function.\n * @returns {*} Returns the updated item, else undefined.\n * @example\n *\n * .jaysn file\n * {\n * \"users\": [\n * { \"user\": \"barney\", \"age\": 36, \"active\": true },\n * { \"user\": \"fred\", \"age\": 40, \"active\": false },\n * { \"user\": \"pebbles\", \"age\": 1, \"active\": true }\n * ]\n * }\n *\n */\n\n new Jaysn('users', {\n user: 'string',\n age: 'number',\n active: 'boolean'\n }).update({\n user: 'bonbon'\n }, o => o.get('age') === 36)\n\n/**\n * // => object for 'bonbon'\n */\n</code></pre>"
}
]
}
16 changes: 4 additions & 12 deletions src/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@
"browser": true,
"node": true
},
"extends": [
"plugin:prettier/recommended"
],
"rules": {
"arrow-parens": ["error", "as-needed"],
"arrow-body-style": ["error", "as-needed"],
"eslint-env": "es6",
"import/no-extraneous-dependencies": [
"error",
{
"optionalDependencies": false,
"peerDependencies": false
}
],
"no-underscore-dangle": ["error", { "allowAfterThis": true }],
"strict": "off"
"no-underscore-dangle": ["error", { "allowAfterThis": true }]
}
}
4 changes: 3 additions & 1 deletion src/adapters/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export class File extends Base {
}

write(data) {
writeFileSync(this.source, JSON.stringify(data, null, 4), { encoding: 'utf8' });
writeFileSync(this.source, JSON.stringify(data, null, 4), {
encoding: 'utf8',
});

return this.read();
}
Expand Down
4 changes: 1 addition & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ export default {
Jaysn,
};

export {
Jaysn,
};
export { Jaysn };
37 changes: 21 additions & 16 deletions src/jaysn.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ const IMMUTABLE_TYPE = [
'Collection',
];

const IMMUTABLE_READ = [
'get',
'getIn',
];
const IMMUTABLE_READ = ['get', 'getIn'];

const IMMUTABLE_WRITE = [
'delete',
Expand All @@ -49,21 +46,27 @@ export class Jaysn {
source: JAYSN_PATH,
};

const defaultData = Immutable.Map(Object.keys(schema).reduce((V, K) => {
const result = V;
result[K] = {};
return result;
}, {})).toJS();
const defaultData = Immutable.Map(
Object.keys(schema).reduce((V, K) => {
const result = V;
result[K] = {};
return result;
}, {})
).toJS();

// Merge options with defaultOptions
const opts = Immutable.fromJS(defaultOpts).merge(Immutable.fromJS(options)).toJS();
const opts = Immutable.fromJS(defaultOpts)
.merge(Immutable.fromJS(options))
.toJS();
let adapter;

// Let's pick the specific adapter
switch (opts.use) {
case 'LocalStorage':
if (typeof localStorage === 'undefined' || localStorage === null) {
throw new TypeError('LocalStorage adapter only available on browser!');
throw new TypeError(
'LocalStorage adapter only available on browser!'
);
}
adapter = new LocalStorage(opts.source);
if (!localStorage.getItem(opts.source)) {
Expand Down Expand Up @@ -134,11 +137,13 @@ export class Jaysn {
const V = data[O];
const L = V.length;
if (
typeof V === 'object'
&& V !== null
&& typeof V !== 'function'
&& typeof L === 'number'
&& L > -1 && L % 1 === 0 && L <= 9007199254740991
typeof V === 'object' &&
V !== null &&
typeof V !== 'function' &&
typeof L === 'number' &&
L > -1 &&
L % 1 === 0 &&
L <= 9007199254740991
) {
V.forEach(NV => {
struct(this.schema[O])(NV);
Expand Down
3 changes: 3 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"env": {
"mocha": true
},
"extends": [
"plugin:prettier/recommended"
],
"rules": {
"import/no-extraneous-dependencies": [
"error",
Expand Down
34 changes: 8 additions & 26 deletions test/Jaysn.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const postData = {
let DB;

describe('new Jaysn()', () => {

before(() => {
if (existsSync(opts.source)) {
unlinkSync(opts.source);
Expand All @@ -58,18 +57,11 @@ describe('new Jaysn()', () => {
});

describe('.set(key, data).write()', () => {

it('should change state when schema check is valid and equals with the inserted data', () => {
DB
.set('users', userData)
.write();
DB
.set('posts', [postData])
.write();
DB.set('users', userData).write();
DB.set('posts', [postData]).write();

DB
.set('posts', DB.get('posts').push(postData))
.write();
DB.set('posts', DB.get('posts').push(postData)).write();

assert.isTrue(DB.get('users').equals(fromJS(userData)));
assert.isTrue(DB.get('posts').equals(fromJS([postData, postData])));
Expand All @@ -79,18 +71,14 @@ describe('new Jaysn()', () => {
userData.id = '1';

assert.throws(
() => DB
.set('users', userData)
.write(),
() => DB.set('users', userData).write(),
TypeError,
'Expected a value of type `number | undefined` for `id` but received `1`.',
'Expected a value of type `number | undefined` for `id` but received `1`.'
);
});

});

describe('.merge(original, other).write()', () => {

it('should change state when schema check is valid and equals with the inserted data', () => {
userData.id = 1;
postData.id = 1;
Expand All @@ -101,9 +89,7 @@ describe('new Jaysn()', () => {
posts: [postData],
});

DB
.merge(origin, other)
.write();
DB.merge(origin, other).write();

assert.isTrue(DB.getState().equals(other));
});
Expand All @@ -117,14 +103,10 @@ describe('new Jaysn()', () => {
});

assert.throws(
() => DB
.merge(origin, other)
.write(),
() => DB.merge(origin, other).write(),
TypeError,
'Expected a value of type `number | undefined` for `id` but received `Hello Jaysn!`.',
'Expected a value of type `number | undefined` for `id` but received `Hello Jaysn!`.'
);
});

});

});
3 changes: 3 additions & 0 deletions tools/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"extends": [
"plugin:prettier/recommended"
],
"rules": {
"import/no-extraneous-dependencies": [
"error",
Expand Down
4 changes: 3 additions & 1 deletion tools/build-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export default {
mkdirSync(DIST_DIR);
}

writeFileSync(join(DIST_DIR, `${pkg.name}.min.js`), result.code, { encoding: 'utf8' });
writeFileSync(join(DIST_DIR, `${pkg.name}.min.js`), result.code, {
encoding: 'utf8',
});
},
},
],
Expand Down
16 changes: 10 additions & 6 deletions tools/build-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ function capitalize(string) {
return [string.charAt(0).toUpperCase(), string.slice(1)].join('');
}

docs.map((o) => {
docs.map(o => {
pages.push({
title: capitalize(o).replace('.md', ''),
content: readFileSync(join(DOCS_PATH, o), 'utf8'),
});
return o;
});

const result = JSON.stringify({
title: capitalize(name),
version,
pages,
}, null, 4);
const result = JSON.stringify(
{
title: capitalize(name),
version,
pages,
},
null,
4
);

writeFileSync(join(RESOURCES_PATH, 'docs.json'), result, 'utf8');
Loading

0 comments on commit 8cbe939

Please sign in to comment.