All files / Nodejs/lib util.js

96.77% Statements 30/31
95.23% Branches 20/21
100% Functions 9/9
96.77% Lines 30/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71    1x 1x 1x   1x 1x   6x       3x       2x 1x   1x       3x 2x   1x       3x     1x 1x 4x   4x 4x     1x 1x 4x 4x         1x     1x             4x 3x   3x 2x       2x    
"use strict";
 
var moment = require("moment");
const fs = require("fs");
const path = require("path");
 
var DATE_FORMAT = "YYYY-MM-DD";
module.exports = {
	dateSafe: function(date) {
		return date ? moment(date).format(DATE_FORMAT) : undefined;
	},
 
	timestampSafe: function(date) {
		return date ? moment(date).format() : undefined;
	},
 
	ifDefined: function(value, func) {
		if (value !== undefined) {
			return func(value);
		}
		return undefined;
	},
 
	boolSafe: function(value, yes, no, none) {
		if (value !== undefined) {
			return value ? yes : no;
		}
		return none;
	},
 
	switchSafe: function(value, values) {
		return values[value] || values.default || values.undefined;
	},
	findParentFiles: function(dir, filename) {
		var paths = [];
		do {
			paths.push(dir);
 
			var lastDir = dir;
			dir = path.resolve(dir, "../");
		} while (dir != lastDir);
 
		var matches = [];
		paths.forEach(function(dir) {
			var file = path.resolve(dir, filename);
			Iif (fs.existsSync(file)) {
 
				matches.push(file);
			}
		});
		return matches;
	},
	toUpperCase: function(value) {
		return value.toUpperCase();
	},
	/**
	 * Extract a portion of a string
	 * @param regex
	 */
	extractStringPart(value, regex) {
		if (value) {
			let returnValue = value.match(regex);
 
			if (returnValue) {
				return returnValue[1] || returnValue[0];
			}
		}
 
		return '';
	}
};