How to use the sanctuary.chain function in sanctuary

To help you get started, we’ve selected a few sanctuary examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Risto-Stevcev / lazy-either / examples / readme2.js View on Github external
//:: String -> String -> String
const join = S.curry2(path.join)

//:: String -> LazyEither (Either e [String])
const ls = path => LazyEither(resolve =>
  fs.readdir(path, (err, files) => resolve(err ? S.Left(err) : S.Right(S.map(join(path), files)))))

//:: String -> LazyEither (Either e String)
const cat = file => LazyEither(resolve =>
  fs.readFile(file, {encoding: 'utf8'}, (err, data) => resolve(err ? S.Left(err) : S.Right(data))))

//:: String -> LazyEither (Either e String)
const catDir =
S.pipe([ls,
        S.chain(S.traverse(LazyEither, cat)),
        S.map(S.unlines)])

// A LazyEither instance is executed when value gets called:
catDir(os.homedir()).value(S.either(console.error, console.log))
github Risto-Stevcev / lazy-either / examples / vs-future1.js View on Github external
{encoding: 'utf8'},
                (err, data) => resolve(err != null ? S.Left(err) : S.Right(data)))
  })

const writeFile = S.curry2((file, data) =>
  new LazyEither(resolve => {
    fs.writeFile(file,
                 data,
                 err => resolve(err != null ? S.Left(err) : S.Right('Write successful')))
  })
)

const getStats =
S.pipe([readDir,
        S.map(S.filter(S.test(/[.]js$/))),
        S.chain(S.traverse(LazyEither, readFile)),
        S.map(String),
        S.map(S.matchAll(/require[(][^)]+[)]/g)),
        S.map(S.prop('length')),
        S.map(String),
        S.map(S.concat('Total requires: ')),
        S.chain(writeFile('stats.txt'))])

getStats(__dirname).value(console.log)
//getStats('blablah').value(console.log)
github Risto-Stevcev / lazy-either / src / either.js View on Github external
LazyEither.prototype['fantasy-land/map'] = function(f) {
  return S.chain(S.compose(LazyEither.Right, f), this)
}
github voorhoede / playbook / src / main.js View on Github external
match,
  map,
  Nothing,
  prop,
  pipe,
  reduce,
} = require('sanctuary');

const foldersToPath = pipe([
  map (compose (kebabCaseIt) (prop('name')) ),
  reduce (curry2(path.join)) (''),
]);

const isPermissionError = pipe([
  gets (Boolean) (['body', 'error_summary']),
  chain(match(/insufficient_permissions/)),
]);

const fetchPaginatedDocIds = apiFetch => previousDocIds => ({
  body: { doc_ids: docIds, cursor, has_more: hasMore },
}) =>
  hasMore
    ? apiFetch('/docs/list/continue', {
      body: { cursor },
    })
      .then(fetchPaginatedDocIds (apiFetch) (previousDocIds.concat(docIds)))
    : previousDocIds.concat(docIds);

const fetchAllDocIds = apiFetch => () => apiFetch('/docs/list', {})
  .then(fetchPaginatedDocIds (apiFetch) ([]));

const fetchDocFolders = apiFetch => docId => apiFetch(