UNPKG

1.75 kBJavaScriptView Raw
1"use strict";
2/**
3 * © 2013 Liferay, Inc. <https://liferay.com> and Node GH contributors
4 * (see file: README.md)
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7Object.defineProperty(exports, "__esModule", { value: true });
8const S = require("sanctuary");
9const Future = require("fluture");
10const fs = require("fs");
11const which = require("which");
12const $ = require("sanctuary-def");
13/* SAFE UTILS */
14exports.safeWhich = Future.encase(which.sync);
15exports.safeRealpath = Future.encase(fs.realpathSync);
16function safeImport(fileName) {
17 let moduleObj = null;
18 try {
19 moduleObj = require(fileName);
20 }
21 catch (e) {
22 return e.code === 'MODULE_NOT_FOUND' ? Future.of(fileName) : Future.reject(e);
23 }
24 return Future.of(moduleObj);
25}
26exports.safeImport = safeImport;
27exports.safeReaddir = Future.encaseN(fs.readdir);
28/* TRANSFORMATIONS */
29// Allows you to take a Maybe returning function and make it an Either returning function
30function maybeFnToEither(monadReturningFunction) {
31 return function convertMaybe(val) {
32 const result = monadReturningFunction(val);
33 return S.isJust(result) ? S.Right(result.value) : S.Left(null);
34 };
35}
36exports.maybeFnToEither = maybeFnToEither;
37/**
38 * Concats two strings
39 * @return {Future}
40 */
41exports.prepend = (a) => (b) => {
42 const argsAreStrings = typeof a === 'string' && typeof b === 'string';
43 return argsAreStrings ? Future.of(a + b) : Future.reject('Both args should be strings');
44};
45/* TYPE CHECKING UTILS */
46exports.isObject = S.is($.Object);
47/* LOGGING UTILS */
48exports.l = x => {
49 console.log('!!!!!!!', x);
50 return x;
51};
52// returns a future
53exports.lf = x => {
54 console.log('-------', x);
55 return Future.of(x);
56};
57//# sourceMappingURL=fp.js.map
\No newline at end of file