UNPKG

definition-tester

Version:
88 lines 2.56 kB
'use strict'; var fs = require('fs'); var Promise = require('bluebird'); var globMod = require('glob'); var grace_fs = require('graceful-fs'); var referenceTagExp = /\/\/\/[ \t]*<reference[ \t]*path=["']?([\w\.\/_-]*)["']?[ \t]*\/>/g; function endsWith(str, suffix) { return str.indexOf(suffix, str.length - suffix.length) !== -1; } exports.endsWith = endsWith; function extractReferenceTags(source) { var ret = []; var match; if (!referenceTagExp.global) { throw new Error('referenceTagExp RegExp must have global flag'); } referenceTagExp.lastIndex = 0; while ((match = referenceTagExp.exec(source))) { if (match.length > 0 && match[1].length > 0) { ret.push(match[1]); } } return ret; } exports.extractReferenceTags = extractReferenceTags; function glob(pattern, opts) { return new Promise(function (resolve, reject) { globMod(pattern, opts || {}, function (err, files) { if (err) { reject(err); } else { resolve(files); } }); }); } exports.glob = glob; function fileExists(target) { return new Promise(function (resolve, reject) { fs.exists(target, function (bool) { resolve(bool); }); }); } exports.fileExists = fileExists; function readFile(target) { return new Promise(function (resolve, reject) { grace_fs.readFile(target, 'utf-8', function (err, contents) { if (err) { reject(err); } else { resolve(contents); } }); }); } exports.readFile = readFile; function readJSON(target) { return readFile(target).then(function (contents) { return JSON.parse(contents); }); } exports.readJSON = readJSON; function filterMapAsync(arr, filterMap) { return Promise.all(arr.map(filterMap)).then(function (ts) { var out = []; for (var _i = 0, ts_1 = ts; _i < ts_1.length; _i++) { var t = ts_1[_i]; if (t !== undefined) { out.push(t); } } return out; }); } exports.filterMapAsync = filterMapAsync; function unique(arr) { var set = {}; for (var _i = 0, arr_1 = arr; _i < arr_1.length; _i++) { var value = arr_1[_i]; set[value] = undefined; } return Object.keys(set); } exports.unique = unique; //# sourceMappingURL=util.js.map