not-node 6.2.15 → 6.2.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/common.js +41 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "not-node",
3
- "version": "6.2.15",
3
+ "version": "6.2.16",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/common.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const fs = require("fs");
2
2
  const path = require("path");
3
3
  const notPath = require("not-path");
4
+ const { rejects } = require("assert");
4
5
 
5
6
  /** @module Common */
6
7
  /**
@@ -205,6 +206,22 @@ module.exports.tryFile = (filePath) => {
205
206
  }
206
207
  };
207
208
 
209
+ /**
210
+ * Asynchronously check file existence and if it's really a file
211
+ * @param {string} filePath full path to file
212
+ * @return {Promise<boolean>} true if path exists and it's a file
213
+ **/
214
+ module.exports.tryFileAsync = (filePath) => {
215
+ return new Promise((resolve, reject) => {
216
+ try {
217
+ const stat = fs.lstatSync(filePath);
218
+ resolve(stat && stat.isFile());
219
+ } catch (e) {
220
+ reject(false);
221
+ }
222
+ });
223
+ };
224
+
208
225
  /**
209
226
  * Trying to parse input to JSON or returns def
210
227
  * @param {string} input string to be parsed
@@ -219,6 +236,30 @@ module.exports.tryParse = (input, def = undefined) => {
219
236
  }
220
237
  };
221
238
 
239
+ /**
240
+ * Trying to asynchronously parse input to JSON or returns def
241
+ * @param {string} input string to be parsed
242
+ * @param {any} def what to return if parse failed, default undefined
243
+ * @return {Promise<Object>} JSON
244
+ **/
245
+ module.exports.tryParseAsync = (
246
+ input,
247
+ def = undefined,
248
+ throwOnException = false
249
+ ) => {
250
+ return new Promise((resolve, reject) => {
251
+ try {
252
+ resolve(JSON.parse(input));
253
+ } catch (e) {
254
+ if (throwOnException && !def) {
255
+ reject(e);
256
+ } else {
257
+ resolve(def);
258
+ }
259
+ }
260
+ });
261
+ };
262
+
222
263
  /**
223
264
  * Generates paths object for module/index.js files based on content and relative
224
265
  * path