mdast
Version:
Markdown processor powered by plugins
184 lines (158 loc) • 4.32 kB
JavaScript
/**
* @author Titus Wormer
* @copyright 2015 Titus Wormer
* @license MIT
* @module mdast:utilities
* @version 2.2.2
* @fileoverview Collection of tiny helpers useful for
* both parsing and compiling markdown.
*/
;
/* eslint-env commonjs */
/*
* Dependencies.
*/
var collapseWhiteSpace = require('collapse-white-space');
/*
* Expressions.
*/
var EXPRESSION_LINE_BREAKS = /\r\n|\r/g;
var EXPRESSION_SYMBOL_FOR_NEW_LINE = /\u2424/g;
var EXPRESSION_BOM = /^\ufeff/;
/**
* Throw an exception with in its `message` `value`
* and `name`.
*
* @param {*} value - Invalid value.
* @param {string} name - Setting name.
*/
function raise(value, name) {
throw new Error(
'Invalid value `' + value + '` ' +
'for setting `' + name + '`'
);
}
/**
* Validate a value to be boolean. Defaults to `def`.
* Raises an exception with `context[name]` when not
* a boolean.
*
* @example
* validateBoolean({foo: null}, 'foo', true) // true
* validateBoolean({foo: false}, 'foo', true) // false
* validateBoolean({foo: 'bar'}, 'foo', true) // Throws
*
* @throws {Error} - When a setting is neither omitted nor
* a boolean.
* @param {Object} context - Settings.
* @param {string} name - Setting name.
* @param {boolean} def - Default value.
*/
function validateBoolean(context, name, def) {
var value = context[name];
if (value === null || value === undefined) {
value = def;
}
if (typeof value !== 'boolean') {
raise(value, 'options.' + name);
}
context[name] = value;
}
/**
* Validate a value to be boolean. Defaults to `def`.
* Raises an exception with `context[name]` when not
* a boolean.
*
* @example
* validateNumber({foo: null}, 'foo', 1) // 1
* validateNumber({foo: 2}, 'foo', 1) // 2
* validateNumber({foo: 'bar'}, 'foo', 1) // Throws
*
* @throws {Error} - When a setting is neither omitted nor
* a number.
* @param {Object} context - Settings.
* @param {string} name - Setting name.
* @param {number} def - Default value.
*/
function validateNumber(context, name, def) {
var value = context[name];
if (value === null || value === undefined) {
value = def;
}
if (typeof value !== 'number' || value !== value) {
raise(value, 'options.' + name);
}
context[name] = value;
}
/**
* Validate a value to be in `map`. Defaults to `def`.
* Raises an exception with `context[name]` when not
* not in `map`.
*
* @example
* var map = {bar: true, baz: true};
* validateString({foo: null}, 'foo', 'bar', map) // 'bar'
* validateString({foo: 'baz'}, 'foo', 'bar', map) // 'baz'
* validateString({foo: true}, 'foo', 'bar', map) // Throws
*
* @throws {Error} - When a setting is neither omitted nor
* in `map`.
* @param {Object} context - Settings.
* @param {string} name - Setting name.
* @param {string} def - Default value.
* @param {Object} map - Enum.
*/
function validateString(context, name, def, map) {
var value = context[name];
if (value === null || value === undefined) {
value = def;
}
if (!(value in map)) {
raise(value, 'options.' + name);
}
context[name] = value;
}
/**
* Clean a string in preperation of parsing.
*
* @example
* clean('\ufefffoo'); // 'foo'
* clean('foo\r\nbar'); // 'foo\nbar'
* clean('foo\u2424bar'); // 'foo\nbar'
*
* @param {string} value - Content to clean.
* @return {string} - Cleaned content.
*/
function clean(value) {
return String(value)
.replace(EXPRESSION_BOM, '')
.replace(EXPRESSION_LINE_BREAKS, '\n')
.replace(EXPRESSION_SYMBOL_FOR_NEW_LINE, '\n');
}
/**
* Normalize an identifier. Collapses multiple white space
* characters into a single space, and removes casing.
*
* @example
* normalizeIdentifier('FOO\t bar'); // 'foo bar'
*
* @param {string} value - Content to normalize.
* @return {string} - Normalized content.
*/
function normalizeIdentifier(value) {
return collapseWhiteSpace(value).toLowerCase();
}
/*
* Expose `validate`.
*/
exports.validate = {
'boolean': validateBoolean,
'string': validateString,
'number': validateNumber
};
/*
* Expose.
*/
exports.normalizeIdentifier = normalizeIdentifier;
exports.clean = clean;
exports.raise = raise;