Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
package-lock.json
.vscode
22 changes: 21 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,24 @@ typecast.array = function (val) {

typecast.boolean = function (val) {
return !! val && val !== 'false' && val !== '0';
};
};

/**
* Cast `val` to `Object`
*
* @param {Mixed} val
* @api public
*/
typecast.object = function(val) {
if (val == null) return {};
if (Array.isArray(val)) return Object.assign({}, val);
if (val instanceof Object) return val;
if (typeof val !== 'string') return { value: val };
let obj = {};
try {
obj = JSON.parse(val);
} catch (error) {
obj = { value: val };
}
return obj;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"test": "mocha --reporter spec --bail"
},
"devDependencies": {
"mocha": "~1.17.0"
"mocha": "^8.1.1"
},
"repository": {
"type": "git",
Expand Down
44 changes: 42 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('.number()', function () {

describe('.date()', function () {
it('should return a date', function () {
assert(typecast.date('2010 10 01').valueOf() === 1285884000000);
assert(typecast.date('2010 10 01').valueOf() === 1285905600000);
})

it('should return default date if typecasting fails', function () {
Expand Down Expand Up @@ -81,6 +81,46 @@ describe('.boolean()', function () {
})
})

describe('.object()', function () {
it('should return an object', function () {
var obj = {x: 1, y: 2, z: 3};
assert(typecast.object(obj) === obj);
assert(typecast.object(1) instanceof Object);
assert(Object.entries(typecast.object('{"x": 1, "y": 2, "z": 3}')).length == 3);
assert(typecast.object('{"x": 1, "y": 2, "z": 3}').z === 3);
})

it('should preserve non-string objects', function () {
var now = new Date();
assert(typeof typecast.object(now) === 'object');
assert(Object.entries(typecast.object(now)).length == 0);
assert(typeof typecast.object(now).getTime === 'function');
});

it('should return an empty object when given a null-ish value', function () {
assert(typeof typecast.object(null) == 'object');
assert(Object.entries(typecast.object(null)).length == 0);

assert(typeof typecast.object(undefined) == 'object');
assert(Object.entries(typecast.object(undefined)).length == 0);
})

it('should return parsed JSON object when given valid JSON', function () {
const validJSON = '{"x": 1, "y": 2, "z": 3}'
assert(typeof typecast.object(validJSON) == 'object');
assert(Object.entries(typecast.object(validJSON)).length == 3);
assert(typecast.object(validJSON).z === 3);
})

it('should return value as property when given non-JSON string or other primitive', function () {
const invalidJSON = 'x: 1, y: 2, z: 3'
assert(typeof typecast.object(invalidJSON) == 'object');
assert(Object.entries(typecast.object(invalidJSON)).length == 1);
assert(typecast.object(invalidJSON).value === 'x: 1, y: 2, z: 3');
assert(typecast.object(1).value === 1);
})
})

describe('typecast()', function () {
it('should work', function () {
assert(typecast(123, 'string') === '123');
Expand All @@ -95,4 +135,4 @@ describe('typecast()', function () {
}
assert(err);
});
});
});