UNPKG

@coolgk/string

Version:
48 lines (46 loc) 1.42 kB
/*! * @package @coolgk/utils * @version 3.1.4 * @link https://github.com/coolgk/node-utils * @license MIT * @author Daniel Gong <daniel.k.gong@gmail.com> * * Copyright (c) 2017 Daniel Gong <daniel.k.gong@gmail.com>. All rights reserved. * Licensed under the MIT License. */ "use strict"; /*! * Copyright (c) 2017 Daniel Gong <daniel.k.gong@gmail.com>. All rights reserved. * Licensed under the MIT License. */ Object.defineProperty(exports, "__esModule", { value: true }); function stripTags(value = '') { return (value + '').replace(/(<([^>]+)>)/ig, ' ').replace(/\s+/g, ' ').trim(); } exports.stripTags = stripTags; function escapeHtml(value = '') { const mappings = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#039;' }; return value ? (value + '').replace(/[&<>"']/g, (matches) => (mappings[matches])) : ''; } exports.escapeHtml = escapeHtml; function unescapeHtml(value = '') { const mappings = { '&amp;': '&', '&lt;': '<', '&gt;': '>', '&quot;': '"', '&#039;': "'" }; return value ? (value + '').replace(/&.+?;/g, (matches) => (mappings[matches] || matches)) : ''; } exports.unescapeHtml = unescapeHtml; function prepad0(value, length = 2) { return (value + '').length > length ? value + '' : ('0'.repeat(length) + value).substr(-length); } exports.prepad0 = prepad0;