UNPKG

simple-git

Version:

Simple GIT interface for node.js

110 lines 3.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const file_exists_1 = require("@kwsites/file-exists"); exports.NOOP = () => { }; /** * Returns either the source argument when it is a `Function`, or the default * `NOOP` function constant */ function asFunction(source) { return typeof source === 'function' ? source : exports.NOOP; } exports.asFunction = asFunction; /** * Determines whether the supplied argument is both a function, and is not * the `NOOP` function. */ function isUserFunction(source) { return (typeof source === 'function' && source !== exports.NOOP); } exports.isUserFunction = isUserFunction; function splitOn(input, char) { const index = input.indexOf(char); if (index <= 0) { return [input, '']; } return [ input.substr(0, index), input.substr(index + 1), ]; } exports.splitOn = splitOn; function first(input, offset = 0) { return isArrayLike(input) && input.length > offset ? input[offset] : undefined; } exports.first = first; function last(input, offset = 0) { if (isArrayLike(input) && input.length > offset) { return input[input.length - 1 - offset]; } } exports.last = last; function isArrayLike(input) { return !!(input && typeof input.length === 'number'); } function toLinesWithContent(input, trimmed = true) { return input.split('\n') .reduce((output, line) => { const lineContent = trimmed ? line.trim() : line; if (lineContent) { output.push(lineContent); } return output; }, []); } exports.toLinesWithContent = toLinesWithContent; function forEachLineWithContent(input, callback) { return toLinesWithContent(input, true).map(line => callback(line)); } exports.forEachLineWithContent = forEachLineWithContent; function folderExists(path) { return file_exists_1.exists(path, file_exists_1.FOLDER); } exports.folderExists = folderExists; /** * Adds `item` into the `target` `Array` or `Set` when it is not already present. */ function append(target, item) { if (Array.isArray(target)) { if (!target.includes(item)) { target.push(item); } } else { target.add(item); } return item; } exports.append = append; function remove(target, item) { if (Array.isArray(target)) { const index = target.indexOf(item); if (index >= 0) { target.splice(index, 1); } } else { target.delete(item); } return item; } exports.remove = remove; exports.objectToString = Object.prototype.toString.call.bind(Object.prototype.toString); function asArray(source) { return Array.isArray(source) ? source : [source]; } exports.asArray = asArray; function asStringArray(source) { return asArray(source).map(String); } exports.asStringArray = asStringArray; function asNumber(source, onNaN = 0) { if (source == null) { return onNaN; } const num = parseInt(source, 10); return isNaN(num) ? onNaN : num; } exports.asNumber = asNumber; //# sourceMappingURL=util.js.map