Skip to content

Commit

Permalink
feat: rework the method, now it works faster on most cases + now it d…
Browse files Browse the repository at this point in the history
…oes not emit most of eslint errors;

fix: tests now meets the requirements of eslint;
  • Loading branch information
xobotyi committed Feb 16, 2020
1 parent 340167b commit a884d61
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 56 deletions.
8 changes: 8 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
"@xobotyi/eslint-config/base",
"@xobotyi/eslint-config/typescript"
],
"rules": {
"prefer-rest-params": "off",
"no-restricted-syntax": [
"error",
"FunctionExpression",
"WithStatement"
]
},
"overrides": [
{
"files": [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dependencies": {},
"devDependencies": {
"@types/jest": "^25.1.2",
"@xobotyi/eslint-config": "^1.2.0",
"@xobotyi/eslint-config": "^1.3.0",
"@xobotyi/preset-typescript": "^1.0.0",
"codacy-coverage": "^3.4.0",
"eslint": "^6.8.0",
Expand Down
73 changes: 46 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,68 @@
export type ClassValue = string | null | boolean | undefined | ClassListDictionary | ClassListArray;

export type ClassListArray = ClassValue[];

export interface ClassListDictionary {
[cn: string]: boolean | undefined | null;
}

export interface ClassListArray extends Array<ClassValue> {}
const { isArray } = Array;

const hasOwnProperty = Object.prototype.hasOwnProperty;
const isArray = Array.isArray;
const toVal = (val: ClassValue): string => {
if (!val) return '';

export default function cnb(...args: ClassListArray): string;
export default function cnb(): string {
const len = arguments.length;
if (typeof val === 'string') return val;

if (!len) return "";
if (typeof val !== 'object') return '';

let str = "",
item,
i,
n;
let str = '';
let tmp;
let l;

for (i = 0; i < len; i++) {
if (!(item = arguments[i])) continue;
if (isArray(val)) {
l = val.length;

if (typeof item === "string") {
str && (str += " "), (str += item);
continue;
}
if (l === 0) return '';

if (l === 1) return toVal(val[0]);

if (typeof item !== "object") continue;
let i = 0;

if (item.push && isArray(item)) {
if ((item = cnb.apply(this, item))) {
str && (str += " "), (str += item);
}
while (i < l) {
tmp = toVal(val[i++]);

continue;
if (tmp) str += (str && ' ') + tmp;
}

for (n in item) {
if (hasOwnProperty.call(item, n) && item[n] && n) {
str && (str += " "), (str += n);
}
return str;
}

for (tmp in val) {
if (val[tmp] && tmp) {
str += (str && ' ') + tmp;
}
}

return str;
};

export default function cnb(...args: ClassListArray): string;
export default function cnb(): string {
const l = arguments.length;

if (l === 0) return '';

if (l === 1) return toVal(arguments[0]);

let i = 0;
let str = '';
let tmp;

while (i < l) {
tmp = toVal(arguments[i++]);

if (tmp) str += (str && ' ') + tmp;
}

return str;
}
58 changes: 30 additions & 28 deletions tests/cnbuilder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,59 @@
import cnb from "../src/index";
import cnb from '../src/index';

describe("cnbuilder", () => {
it("should build from strings", () => {
expect(cnb("foo", "bar", "baz")).toBe("foo bar baz");
describe('cnbuilder', () => {
it('should build from strings', () => {
expect(cnb('foo', 'bar', 'baz')).toBe('foo bar baz');
});

it("should build from array", () => {
expect(cnb(["foo", "bar", "baz"])).toBe("foo bar baz");
it('should build from array', () => {
expect(cnb(['foo', 'bar', 'baz'])).toBe('foo bar baz');
});

it("should build from nested arrays", () => {
expect(cnb(["foo", ["bar", "baz"]])).toBe("foo bar baz");
it('should build from nested arrays', () => {
expect(cnb(['foo', ['bar', 'baz']])).toBe('foo bar baz');
});

it("should build from objects", () => {
it('should build from objects', () => {
expect(
cnb({
foo: true,
bar: true,
baz: true,
bax: false
})
).toBe("foo bar baz");
bax: false,
}),
).toBe('foo bar baz');
});

it("should build from mixed args", () => {
expect(cnb("foo", ["bar", { baz: true, bux: false }], { bax: true, abc: false })).toBe("foo bar baz bax");
it('should build from mixed args', () => {
expect(cnb('foo', ['bar', { baz: true, bux: false }], { bax: true, abc: false })).toBe('foo bar baz bax');
});

it("should ignore wrong args", () => {
it('should ignore wrong args', () => {
expect(
cnb(
//@ts-ignore
"foo",
["bar", { baz: true, bux: false }],
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
'foo',
['bar', { baz: true, bux: false }],
{
bax: true,
abc: false
abc: false,
},
false,
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
123,
() => {}
)
).toBe("foo bar baz bax");
// eslint-disable-next-line @typescript-eslint/no-empty-function
() => {},
),
).toBe('foo bar baz bax');
});

it("should return empty string on empty call", () => {
//@ts-ignore
expect(cnb()).toBe("");
it('should return empty string on empty call', () => {
expect(cnb()).toBe('');
});

it("should ignore empty strings, empty arrays, empty build results (not add extra spaces)", () => {
//@ts-ignore
expect(cnb([""], "foo", { "": true }, "bar", "", "baz", [])).toBe("foo bar baz");
it('should ignore empty strings, empty arrays, empty build results (not add extra spaces)', () => {
expect(cnb([''], 'foo', { '': true }, 'bar', '', 'baz', [])).toBe('foo bar baz');
});
});

0 comments on commit a884d61

Please sign in to comment.