package
Convert strings to JavaScript types (boolean, null, number, object, array)
npm install tiny-coerceGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
14.8 kB
Includes dependencies.
Tiny coercion library for Client or Server. Converts string values to their appropriate JavaScript types (boolean, null, undefined, number, object, array).
Great for DOM data attributes, localStorage, and other cases where you need to store strings that represent typed values.
npm install tiny-coerceimport { coerce } from "tiny-coerce";const { coerce } = require("tiny-coerce");import { coerce } from "tiny-coerce";
// Basic coercion
coerce("true"); // true
coerce("false"); // false
coerce("null"); // null
coerce("undefined"); // undefined
coerce("123"); // 123
coerce("3.14"); // 3.14
// JSON coercion
coerce('{"a":1}'); // {a: 1}
coerce("[1,2,3]"); // [1, 2, 3]
coerce('"hello"'); // "hello"
// With options
coerce(arg, options = {})Coerces a string value to its appropriate JavaScript type.
Parameters:
arg {*} - The value to coerceoptions {Object} (default: {}) - Coercion options
maxStringSize {number} (default: 100000) - Maximum string size in bytesReturns: {*} - The coerced value
Errors:
Throws an Error if:
maxStringSizeCoerced Types:
| Input | Output | Notes |
|---|---|---|
"true", "TRUE", "True" | true | Case-insensitive |
"false", "FALSE", "False" | false | Case-insensitive |
"null", "NULL", "Null" | null | Case-insensitive |
"undefined" | undefined | Exact match |
"123", "3.14" | 123, 3.14 | Any numeric string |
'{"a":1}' | {a: 1} | Objects and arrays |
Tiny Coerce has 100% code coverage with its tests.
$ npm run coverage
ℹ start of coverage report
ℹ ----------------------------------------------------------
ℹ file | line % | branch % | funcs % | uncovered lines
ℹ ----------------------------------------------------------
ℹ ----------------------------------------------------------
ℹ all files | 100.00 | 100.00 | 100.00 |
ℹ ----------------------------------------------------------
ℹ end of coverage reportSee coverage.txt for the latest coverage report.
See docs/TECHNICAL.md for in-depth technical documentation covering:
git checkout -b feature/amazing-feature)npm test)git commit --no-verify -m 'Add amazing feature')git push origin feature/amazing-feature)Copyright (c) 2026 Jason Mulligan
Licensed under the BSD-3-Clause license
'"hello"'"hello" |
| JSON strings |