Tag: ramda.js

从一系列可能的path中获取string

我有一个场景,我需要抓住一个对象的string的第一个匹配项,但只有匹配发生在一个已经预先定义的path中。 { id: 'I60ODI', description: 'some random description' } { foo: 'bar', description: { color: 'green', text: 'some description within text' } } 当提供上述两个对象中的任何一个时,我希望解决scheme返回some random description或some description within text ,只要两个可能的path是obj.description和obj.description.text 。 未来可能还需要添加新的path,所以添加它们需要很容易。 这是迄今为止我已经实施的解决scheme,但对我来说,这似乎并不是最佳的。 // require the ramda library const R = require('ramda'); // is the provided value a string? const isString = R.ifElse(R.compose(R.equals('string'), (val) => […]

调用ramda组成nodejs类

我有以下方法skipLoggingThisRequest在我试图testingnode js类。 该方法应该返回true或false ,根据请求中的path,使用ramda compose来获得该值。 但是在我的testing中,无论我在请求对象中设置了什么path,我的skipLoggingThisRequest总是返回true。 我在这里错过了什么? 我的课: import { compose, filter, join, toPairs, map, prop, flip, contains, test, append } from 'ramda' import { create, env } from 'sanctuary' import { isEmpty, flattenDeep } from 'lodash' import chalk from 'chalk' import log from 'menna' class MyClass { constructor (headerList) { this.headerWhiteList = flattenDeep(append(headerList, [])); […]

Nodejs – 通过添加具有相同键的值来返回键值对的数组

我有以下的对象数组: const list = [ {points: 3, name:'bob'}, {points: 1, name:'john'}, {points: 5, name:'john'}, {points: 2, name:'john'}, {points: 8, name:'john'}, {points: 0, name:'bob'}, {points: 2, name:'bob'}, {points: 1, name:'bob'}, ] 我想结束,并返回一个包含每个名称点的总和的数据集列表,如: const list2 = [ {name: 'bob', points: 6}, // sum of all of bob's points {name: 'john', points: 16} ] 我需要这个,所以我可以使用生成的数据集来生成折线图。 有没有办法做到这一点,最好使用ramda.js ?

Monadic IO与ramda和ramda幻想

试图找出IO monad是如何工作的。 使用下面的代码,我读取filenames.txt并使用结果来重命名目录testfiles中的文件。 这显然是未完成的,所以而不是实际重命名我login到控制台的任何东西。 🙂 我的问题是: 我打电话runIO两次,但它感觉只是应该只调用一次,到底? 我想使用renameIO而不是renaneDirect但无法find正确的语法。 任何其他build议也表示赞赏,我是新来的FP! var R = require('ramda'); var IO = require('ramda-fantasy').IO var fs = require('fs'); const safeReadDirSync = dir => IO(() => fs.readdirSync(dir)); const safeReadFileSync = file => IO(() => fs.readFileSync(file, 'utf-8')); const renameIO = (file, name) => IO(() => console.log('Renaming file ' + file + ' to ' […]

使用Ramda处理asynchronous编程

我正在寻找处理函数,返回承诺与拉普达function,然后pipeP。 我试图比较函数(其中之一返回一个承诺)与这样的等于: getSectionFromDb :: obj -> promise getSectionFromData :: obj -> number R.equals( getSectionFromDb, getSectionFromData ) 这里有两个因素。 首先R.equals不会评估函数,但更大的问题是,我将一个承诺与一个数字进行比较。 有没有这样的东西(我知道function不参考透明,但必须有办法处理IO)的function方式? 有没有拉姆达这样做的方式? 谢谢。