Skip to content

fix: improve type test #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
const is = (val, type) => Object.prototype.toString.call(val) === `[object ${type}]`;

function toVal(mix) {
var k, y, str='';

if (typeof mix === 'string' || typeof mix === 'number') {
if (is(mix, 'String') || is(mix, 'Number')) {
str += mix;
} else if (typeof mix === 'object') {
if (Array.isArray(mix)) {
} else if (is(mix, 'Array')) {
var len=mix.length;
for (k=0; k < len; k++) {
if (mix[k]) {
if (mix[k] && mix[k].valueOf()) {
if (y = toVal(mix[k])) {
str && (str += ' ');
str += y;
}
}
}
} else {
for (y in mix) {
if (mix[y]) {
str && (str += ' ');
str += y;
}
} else if (is(mix, 'Object')) {
for (y in mix) {
if (mix[y] && mix[y].valueOf()) {
str && (str += ' ');
str += y;
}
}
}
Expand All @@ -30,7 +30,7 @@ function toVal(mix) {
export function clsx() {
var i=0, tmp, x, str='', len=arguments.length;
for (; i < len; i++) {
if (tmp = arguments[i]) {
if ((tmp = arguments[i]) && tmp.valueOf()) {
if (x = toVal(tmp)) {
str && (str += ' ');
str += x
Expand Down
2 changes: 1 addition & 1 deletion src/lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export function clsx() {
var i=0, tmp, str='', len=arguments.length;
for (; i < len; i++) {
if (tmp = arguments[i]) {
if (typeof tmp === 'string') {
if (Object.prototype.toString.call(tmp) === '[object String]') {
str += (str && ' ') + tmp;
}
}
Expand Down
29 changes: 29 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ test('strings (variadic)', () => {
assert.is(fn(false && 'foo', 'bar', 'baz', ''), 'bar baz');
});

test('string instances', () => {
assert.is(fn(new String('')), '');
assert.is(fn(new String('foo')), 'foo');
assert.is(fn(true && new String('foo')), 'foo');
assert.is(fn(false && new String('foo')), '');
});

test('string instances (variadic)', () => {
assert.is(fn(new String('')), '');
assert.is(fn(new String('foo'), new String('bar')), 'foo bar');
assert.is(fn(true && new String('foo'), false && new String('bar'), new String('baz')), 'foo baz');
assert.is(fn(false && new String('foo'), new String('bar'), new String('baz'), ''), 'bar baz');
});

test('numbers', () => {
assert.is(fn(1), '1');
assert.is(fn(12), '12');
Expand All @@ -43,6 +57,21 @@ test('numbers (variadic)', () => {
assert.is(fn(1, 2), '1 2');
});

test('number instances', () => {
assert.is(fn(new Number(1)), '1');
assert.is(fn(new Number(12)), '12');
assert.is(fn(new Number(0.1)), '0.1');
assert.is(fn(new Number(0)), '');

assert.is(fn(new Number(Infinity)), 'Infinity');
assert.is(fn(new Number(NaN)), '');
});

test('number instances (variadic)', () => {
assert.is(fn(new Number(0), new Number(1)), '1');
assert.is(fn(new Number(1), new Number(2)), '1 2');
});

test('objects', () => {
assert.is(fn({}), '');
assert.is(fn({ foo:true }), 'foo');
Expand Down
14 changes: 14 additions & 0 deletions test/lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ test('strings (variadic)', () => {
assert.is(fn(false && 'foo', 'bar', 'baz', ''), 'bar baz');
});

test('string instances', () => {
assert.is(fn(new String('')), '');
assert.is(fn(new String('foo')), 'foo');
assert.is(fn(true && new String('foo')), 'foo');
assert.is(fn(false && new String('foo')), '');
});

test('string instances (variadic)', () => {
assert.is(fn(new String('')), '');
assert.is(fn(new String('foo'), new String('bar')), 'foo bar');
assert.is(fn(true && new String('foo'), false && new String('bar'), new String('baz')), 'foo baz');
assert.is(fn(false && new String('foo'), new String('bar'), new String('baz'), ''), 'bar baz');
});

test('emptys', () => {
assert.is(fn(''), '');
assert.is(fn(undefined), '');
Expand Down