Light ECMAScript Value Notation (LEVN)
Levn is a JavaScript library for parsing human-readable string input into structured JavaScript values, leveraging type validation. It is designed for concise, human-authored data, such as configuration files or command-line arguments, distinguishing itself from JSON by prioritizing human readability, conciseness, and built-in type validation via `type-check`. It supports regex and date literals and is extensible with custom types. Unlike JSON, it is not optimized for large-scale data transport due to its parsing overhead. The current stable version is 0.4.1. This package appears to be largely unmaintained, with its last npm publish over six years ago and minimal activity on its GitHub repository in recent years.
Common errors
-
TypeError: require is not a function
cause Attempting to use `require` in an ECMAScript Module (ESM) context without proper configuration.fixEnsure your project is configured for CommonJS, or use dynamic `import()` if absolutely necessary within an ESM file. `levn` is designed for CommonJS (`require`). -
Error: Cannot find module 'type-check'
cause `type-check`, a core dependency, is missing from `node_modules`.fixInstall the peer dependency: `npm install type-check`. -
SyntaxError: Unexpected token '...' (or similar parsing errors)
cause Input string does not conform to Levn's syntax rules or the specified type, or contains unrecognized literals.fixReview the Levn format documentation and ensure the input string correctly matches the `type` string provided to the `parse` function. Pay attention to delimiters and value formats (e.g., dates with `#`, regex with `/`). -
TypeError: Cannot read properties of undefined (reading 'parse')
cause The `levn` module was not successfully imported, or `require('levn')` returned `undefined` or an unexpected value.fixVerify that `levn` is installed (`npm list levn`) and that the `require` path is correct. Ensure no other module is inadvertently shadowing `levn`.
Warnings
- breaking Levn is an abandoned package. Its last major update was in 2013-2014, and the last npm publish was six years ago. It relies on very old Node.js versions (>=0.8.0). Use in modern environments without careful consideration and potential polyfills is not recommended and may lead to compatibility issues or security vulnerabilities through its dependencies.
- gotcha Levn is a CommonJS-only module. Attempting to use `import` statements (ESM syntax) will result in runtime errors in Node.js environments unless transpiled or configured correctly with a CommonJS wrapper.
- gotcha The library explicitly states it is slower and less efficient than JSON for transporting large amounts of data. Its purpose is for human-entered, concise data, not machine-to-machine communication.
- gotcha Levn's parsing heavily relies on the `type-check` library for its type format and validation. Developers need to understand `type-check`'s syntax and capabilities to effectively use `levn` for complex data structures.
Install
-
npm install levn -
yarn add levn -
pnpm add levn
Imports
- parse
import { parse } from 'levn';const { parse } = require('levn'); - parsedTypeParse
import parsedTypeParse from 'levn/parsedTypeParse';
const { parsedTypeParse } = require('levn'); - VERSION
const VERSION = require('levn').VERSION;const { VERSION } = require('levn');
Quickstart
const { parse } = require('levn');
const { parseType } = require('type-check'); // Assuming 'type-check' is installed
// Basic type parsing
console.log('Number 2:', parse('Number', '2'));
console.log('String "levn":', parse('String', 'levn'));
console.log('Boolean true:', parse('Boolean', 'true'));
// Complex types and implicit delimiters
console.log('Array of Numbers:', parse('[Number]', '1,2,3'));
console.log('Object with properties:', parse('{a: String, b: Number}', 'a: str, b: 2'));
// Parsing with pre-parsed types (from type-check)
const parsedNumberArrayType = parseType('[Number]');
console.log('Parsed Type Array:', parse('parsedTypeParse', parsedNumberArrayType, '4,5,6'));