-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"use strict"; | ||
|
||
var isValue = require("../value/is"); | ||
|
||
module.exports = function (value) { | ||
if (!isValue(value)) return null; | ||
try { | ||
return "" + value; // Ensureimplicit coercion | ||
} catch (error) { | ||
return null; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"use strict"; | ||
|
||
var assert = require("chai").assert | ||
, stringCoerce = require("../../string/coerce"); | ||
|
||
describe("string/coerce", function () { | ||
it("Should return input string", function () { assert.equal(stringCoerce("foo"), "foo"); }); | ||
it("Should coerce numbers", function () { assert.equal(stringCoerce(12), "12"); }); | ||
it("Should coerce booleans", function () { assert.equal(stringCoerce(true), "true"); }); | ||
it("Should coerce string objects", function () { | ||
assert.equal(stringCoerce(new String("bar")), "bar"); | ||
}); | ||
it("Should coerce objects", function () { | ||
assert.equal( | ||
stringCoerce({ toString: function () { return "Some object"; } }), "Some object" | ||
); | ||
}); | ||
it("Should not coerce null", function () { assert.equal(stringCoerce(null), null); }); | ||
it("Should not coerce undefined", function () { assert.equal(stringCoerce(undefined), null); }); | ||
|
||
if (typeof Symbol === "function") { | ||
it("Should not coerce symbols", function () { | ||
// eslint-disable-next-line no-undef | ||
assert.equal(stringCoerce(Symbol()), null); | ||
}); | ||
} | ||
}); |