Skip to content

Commit

Permalink
Add eslint, change prettier config (#53)
Browse files Browse the repository at this point in the history
* Add eslint, change prettier config

* Use Object.assign to support old vesion
  • Loading branch information
strdr4605 authored Aug 29, 2019
1 parent 7f3c2b9 commit 2b8db96
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 48 deletions.
29 changes: 29 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = {
env: {
browser: true,
commonjs: true,
es6: true,
node: true,
jest: true,
},
extends: ["airbnb-base"],
globals: {
Atomics: "readonly",
SharedArrayBuffer: "readonly",
},
parserOptions: {
ecmaVersion: 2018,
},
plugins: ["jest"],
rules: {
"no-extend-native": 0,
"no-console": 0,
"max-len": ["error", { code: 120, ignoreComments: true, ignoreStrings: true }],
quotes: 0,
"no-use-before-define": 0,
"no-plusplus": 0,
"no-param-reassign": 1,
"arrow-parens": [2, "as-needed"],
"prefer-object-spread": 0,
},
};
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
4 changes: 2 additions & 2 deletions . prettierrc.json → .prettierrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"trailingComma": "es5",
"trailingComma": "all",
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": false,
"printWidth": 80
"printWidth": 120
}
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ declare module "mockingcase" {
namespace mockingcase {
type MockingCase = (
input: string,
options?: Options
options?: Options,
) => string & {
log: (input: string, options?: Options) => void;
config: (defaultOptions: Options) => MockingCase;
Expand Down
29 changes: 9 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use strict";

/**
* Converts the input string(s) to mOcKiNgCaSe.
* @param {(string | string[])} input String(s) to be converted.
Expand All @@ -15,9 +13,9 @@ function mOcKiNgCaSe(input = "", options) {
{
random: false,
onlyLetters: false,
firstUpper: false
firstUpper: false,
},
options
options,
);

// Combine strings first to form the input string.
Expand Down Expand Up @@ -46,9 +44,8 @@ function mOcKiNgCaSe(input = "", options) {

if (options.firstUpper) {
return convert(input, (str, i) => i % 2 === 0);
} else {
return convert(input, (str, i) => i % 2 === 1);
}
return convert(input, (str, i) => i % 2 === 1);
}

/**
Expand All @@ -70,11 +67,9 @@ mOcKiNgCaSe.log = (input, options) => console.log(mOcKiNgCaSe(input, options));
* @param {boolean} defaultOptions.firstUpper=false - If the first letter should be capitalized instead of the second when converting to mOcKiNgCaSe (e.g. MoCkInGcAsE). * @returns {string} string in mOcKiNgCaSe
* @return mOcKiNgCaSe with default options
*/
mOcKiNgCaSe.config = defaultOptions => {
return (input = "", overridedDefaultOptions) => {
const options = overridedDefaultOptions || defaultOptions;
return mOcKiNgCaSe(input, options);
};
mOcKiNgCaSe.config = defaultOptions => (input = "", overridedDefaultOptions) => {
const options = overridedDefaultOptions || defaultOptions;
return mOcKiNgCaSe(input, options);
};

/**
Expand All @@ -92,7 +87,7 @@ mOcKiNgCaSe.overrideString = () => {
* @returns {string} string in mOcKiNgCaSe
* @see mOcKiNgCaSe
*/
String.prototype.toMockingCase = function(options) {
String.prototype.toMockingCase = function toMockingCase(options) {
return mOcKiNgCaSe(this, options);
};

Expand Down Expand Up @@ -128,9 +123,7 @@ function isArrayOfStrings(input) {

input.forEach((value, i) => {
if (typeof value !== "string") {
throw TypeError(
`Expected array of strings but got type '${typeof value}' at index ${i}`
);
throw TypeError(`Expected array of strings but got type '${typeof value}' at index ${i}`);
}
});

Expand All @@ -153,11 +146,7 @@ function randomCase(input) {
* @return The converted string.
*/
function convert(input, shouldLetterBeUpperCase) {
return input.replace(/./g, (str, i) => {
return shouldLetterBeUpperCase(str, i)
? str.toUpperCase()
: str.toLowerCase();
});
return input.replace(/./g, (str, i) => (shouldLetterBeUpperCase(str, i) ? str.toUpperCase() : str.toLowerCase()));
}

module.exports = mOcKiNgCaSe;
37 changes: 14 additions & 23 deletions mockingcase.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-shadow */
/* eslint-disable global-require */
const mOcKiNgCaSe = require("./index").overrideString();

describe("mockingcase", () => {
Expand All @@ -18,21 +20,19 @@ describe("mockingcase", () => {
describe(`"Random" conversion option enabled`, () => {
const input = "hello world";
const options = {
random: true
random: true,
};
const normalOutput = mOcKiNgCaSe(input);
const randomlyConvertedOutput = mOcKiNgCaSe(input, options);

test("Returned string should equal input string when ignoring case", () => {
expect(normalOutput.toLowerCase()).toStrictEqual(
randomlyConvertedOutput.toLowerCase()
);
expect(normalOutput.toLowerCase()).toStrictEqual(randomlyConvertedOutput.toLowerCase());
});

test(`"First upper" option will capitalize the first letter`, () => {
const options = {
random: true,
firstUpper: true
firstUpper: true,
};
// Generate 100 random strings and ensure all have an uppercase first letter.
for (let i = 0; i < 100; i++) {
Expand All @@ -45,7 +45,7 @@ describe("mockingcase", () => {
describe(`"First upper" option enabled`, () => {
const input = "hello world";
const options = {
firstUpper: true
firstUpper: true,
};
const output = mOcKiNgCaSe(input, options);
const expectedOutput = "HeLlO WoRlD";
Expand All @@ -58,10 +58,7 @@ describe("mockingcase", () => {
describe("empty input", () => {
test("If the input is left blank, an error should be thrown", () => {
const input = "";
const expectedOutput = undefined;
expect(() => mOcKiNgCaSe(input)).toThrowError(
new Error("An input is required")
);
expect(() => mOcKiNgCaSe(input)).toThrowError(new Error("An input is required"));
});
});

Expand Down Expand Up @@ -103,7 +100,7 @@ describe("mockingcase", () => {
test("Does not ignore options", () => {
const input = "hello world";
const options = {
firstUpper: true
firstUpper: true,
};
const expectedOutput = mOcKiNgCaSe(input, options);
expect(input.toMockingCase(options)).toMatch(expectedOutput);
Expand All @@ -119,7 +116,7 @@ describe("mockingcase", () => {
test("string with number characters", () => {
const input = "hello world123";
const options = {
onlyLetters: true
onlyLetters: true,
};
const expectedOutput = "hElLo wOrLd";
expect(mOcKiNgCaSe(input, options)).toEqual(expectedOutput);
Expand All @@ -128,7 +125,7 @@ describe("mockingcase", () => {
test("string with symbols", () => {
const input = "hello$%@# %world@";
const options = {
onlyLetters: true
onlyLetters: true,
};
const expectedOutput = "hElLo wOrLd";
expect(mOcKiNgCaSe(input, options)).toEqual(expectedOutput);
Expand All @@ -147,7 +144,7 @@ describe("mockingcase", () => {
const mOcKiNgCaSe = require("./index").config({ onlyLetters: true });
const input = "hello123";
const options = {
onlyLetters: false
onlyLetters: false,
};
const expectedOutput = "hElLo123";
expect(mOcKiNgCaSe(input, options)).toEqual(expectedOutput);
Expand All @@ -157,22 +154,16 @@ describe("mockingcase", () => {
describe("default input", () => {
test("If the input is undefined, an error should be thrown", () => {
const input = undefined;
const expectedOutput = undefined;
expect(() => mOcKiNgCaSe(input)).toThrow(
new Error("An input is required")
);
expect(() => mOcKiNgCaSe(input)).toThrow(new Error("An input is required"));
});

test("For config option if the input is undefined, an error should be thrown", () => {
const mOcKiNgCaSe = require("./index").config({ onlyLetters: true });
const input = undefined;
const options = {
onlyLetters: false
onlyLetters: false,
};
const expectedOutput = undefined;
expect(() => mOcKiNgCaSe(input, options)).toThrow(
new Error("An input is required")
);
expect(() => mOcKiNgCaSe(input, options)).toThrow(new Error("An input is required"));
});
});
describe("overrideConsole", () => {
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
},
"scripts": {
"test": "jest --coverage",
"format": "prettier --write \"*.[jt]s\"",
"pre-commit": "npm run format && npm test"
"format": "prettier --config .prettierrc --write \"*.[jt]s\"",
"lint": "eslint index.js mockingcase.test.js",
"pre-commit": "npm run format && npm run lint && npm test"
},
"husky": {
"hooks": {
Expand All @@ -35,6 +36,10 @@
"author": "Dragoș Străinu",
"license": "ISC",
"devDependencies": {
"eslint": "^6.2.2",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jest": "^22.15.2",
"husky": "^2.4.1",
"jest": "^23.6.0",
"prettier": "1.18.2"
Expand Down

0 comments on commit 2b8db96

Please sign in to comment.