UNPKG

@11ty/gray-matter

Version:

Parse front-matter from a string or file. Fast, reliable and easy to use. Parses YAML front matter by default, but also has support for YAML, JSON, TOML Front-Matter, with options to set custom delimiters. Used by metalsmith, assemble, verb and many other

86 lines (67 loc) 1.68 kB
'use strict'; exports.define = function(obj, key, val) { Reflect.defineProperty(obj, key, { enumerable: false, configurable: true, writable: true, value: val }); }; /** * Returns true if `val` is a buffer */ exports.isBuffer = function(val) { return val instanceof Uint8Array; }; const toString = Object.prototype.toString; /** * Returns true if `val` is an object */ exports.isObject = function(val) { return toString.call(val) === '[object Object]'; }; /** * Cast `input` to a buffer */ exports.toBuffer = function(input) { if(typeof input === 'string') { if(typeof TextEncoder !== "undefined") { return new TextEncoder().encode(input); } throw new Error("Unsupported environment: TextEncoder global required."); } return input; }; function stripBom(str) { return (typeof str === 'string' && str.charAt(0) === '\ufeff') ? str.slice(1) : str; } /** * Cast `val` to a string. */ exports.toString = function(input) { if (exports.isBuffer(input)) { if(typeof TextDecoder !== "undefined") { return stripBom(new TextDecoder().decode(input)); } throw new Error("Unsupported environment: TextDecoder required."); } if (typeof input !== 'string') { throw new TypeError('expected input to be a string or buffer'); } return stripBom(input); }; /** * Cast `val` to an array. */ exports.arrayify = function(val) { return val ? (Array.isArray(val) ? val : [val]) : []; }; /** * Returns true if `str` starts with `substr`. */ exports.startsWith = function(str, substr, len) { if (typeof len !== 'number') len = substr.length; return str.slice(0, len) === substr; };