How to use the view function from ramda
Find comprehensive JavaScript ramda.view code examples handpicked from public code repositorys.
ramda.view is a function that takes a property and an object and returns the value of the specified property in the object.
GitHub: alexa/ask-cli
22 23 24 25 26 27 28 29 30 31 32
if (awsAccessKeyId && awsSecretAccessKey) { return CONSTANT.PLACEHOLDER.ENVIRONMENT_VAR.AWS_CREDENTIALS; } } const askConfig = fs.readJSONSync(path.join(os.homedir(), ".ask", "cli_config")); return R.view(R.lensPath(["profiles", askProfile, "aws_profile"]), askConfig); } // We face the same issue as mentioned in https://github.com/aws/aws-sdk-js/issues/2181 // Issue is about when we instantiate the aws-sdk, the region value is not passed in correctly based on the profile.
GitHub: alexa/ask-cli
61 62 63 64 65 66 67 68
const configFile = path.join(os.homedir(), ".ask", "cli_config"); if (!fs.existsSync(configFile)) { throw new Error('The app config for ask-cli does not exist. Please run "ask configure" to complete the CLI initialization.'); } else { const cliConfig = JSON.parse(fs.readFileSync(configFile, "utf-8")); return R.view(R.lensPath(["profiles", profile, "vendor_id"]), cliConfig); } }
How does ramda.view work?
ramda.view
is a function that takes a property path and a target object, and returns the value at the end of the property path from the target object.
For example, given a property path ['a', 'b']
and a target object {a: {b: 1}}
, ramda.view(['a', 'b'], {a: {b: 1}})
returns 1
.
GitHub: alexa/ask-cli
70 71 72 73 74 75 76 77 78 79
}; httpClient.request(options, "GET_ACCESS_TOKEN_USING_REFRESH_TOKEN", this.config.doDebug, (err, response) => { if (err) { return callback(err); } const responseErr = R.view(R.lensPath(["body", "error"]), response); if (stringUtils.isNonBlankString(responseErr)) { return callback(`Refresh LWA tokens failed, please run "ask configure" to manually update your tokens. Error: ${responseErr}.`); } const expiresIn = R.view(R.lensPath(["body", "expires_in"]), response);
GitHub: alexa/ask-cli
50 51 52 53 54 55 56 57 58 59
interactionModel: {}, hostedSkillProvisioning: {}, }; const shouldRetryCondition = (retryResponse) => { const response = retryResponse.body; statusTracker.manifest = R.view(R.lensPath([CONSTANTS.HOSTED_SKILL.RESOURCES.MANIFEST, "lastUpdateRequest", "status"]), response); if (response[CONSTANTS.HOSTED_SKILL.RESOURCES.INTERACTION_MODEL]) { const locale = Object.keys(response[CONSTANTS.HOSTED_SKILL.RESOURCES.INTERACTION_MODEL])[0]; statusTracker.interactionModel = R.view( R.lensPath([CONSTANTS.HOSTED_SKILL.RESOURCES.INTERACTION_MODEL, locale, "lastUpdateRequest", "status"]),
+ 2 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12
import R from "ramda"; const user = { name: "Alice", address: { city: "London", postcode: "SW1A 1AA", }, }; const cityLens = R.lensPath(["address", "city"]); const userCity = R.view(cityLens, user); // returns 'London'
In this example, we define an object user with a name property and an address property that is itself an object with city and postcode properties. We then create a lens with R.lensPath that focuses on the city property of the address object. Finally, we use R.view to get the value of city for the user object, which returns 'London'.
GitHub: alexa/ask-cli
349 350 351 352 353 354 355 356 357 358
} loopCallback(null, statusResponse); }); }; const shouldRetryCondition = (retryResponse) => R.view(R.lensPath(["body", "manifest", "lastUpdateRequest", "status"]), retryResponse) === CONSTANTS.SKILL.SKILL_STATUS.IN_PROGRESS; retryUtils.retry(retryConfig, retryCall, shouldRetryCondition, (err, res) => callback(err, err ? null : res)); } /**
GitHub: alexa/ask-cli
50 51 52 53 54 55 56 57 58 59
*/ function extractUpgradeInformation(v1Config, profile) { // 1.check v1 .ask/config exists const v1ProjData = { skillId: R.view(R.lensPath(["deploy_settings", profile, "skill_id"]), v1Config), isHosted: R.view(R.lensPath(["alexaHosted", "isAlexaHostedSkill"]), v1Config) || false, lambdaList: R.view(R.lensPath(["deploy_settings", profile, "resources", "lambda"]), v1Config) || [], }; // 2.check if skillId exists if (!stringUtils.isNonBlankString(v1ProjData.skillId)) {
+ 29 other calls in file
70 71 72 73 74 75 76 77 78 79
* Get property based on the property array. * Return undefined if not found. * @param {string} pathArray e.g. ['path', 'to', 'the', '3rd', 'object', 2, 'done'] */ getProperty(pathArray) { return R.view(R.lensPath(pathArray), this.content); } /** * Set property to the runtime object based on the property array.
+ 4 other calls in file
GitHub: alexa/ask-cli
447 448 449 450 451 452 453 454 455
if (pollErr) { return loopCallback(pollErr); } // print skill Id if needed const skillId = R.view(R.lensPath(["body", "skill", "skillId"]), pollResponse); if (skillId) { this.getImportStatusPollView().displaySkillId(skillId, false); }
+ 15 other calls in file
GitHub: 0xjjpa/github-patterns
281 282 283 284 285 286 287 288 289 290
projects = R.flatten(nestedProjects); // const projectsByMonth = R.reduce( // (accum, project) => R.set( // dateLens(project), // R.view(dateLens(project), accum) ? // R.view(dateLens(project), accum).concat(project) : // [].concat(project), // accum // ),
+ 3 other calls in file
228 229 230 231 232 233 234 235 236 237
const getLabel = graphObj => { const graphData = graphObj.data; const lens = isCompoundState(graphObj) ? compoundStateLens : atomicStateLens; const dataKeys = Array.isArray(graphData) ? graphData.reduce((acc, dataItem) => { return Object.assign(acc, {[dataItem['@_key']]: view(lens, dataItem)}) },{}) : graphData['@_key'] === 'd6' ? {d6: view(lens, graphData)} : {};
+ 3 other calls in file
GitHub: corporatepiyush/es6
237 238 239 240 241 242 243 244 245 246 247 248
{ // lenses const xLens = _.lens(_.prop('x'), _.assoc('x')) _.view(xLens, { x: 1, y: 2 }) //=> 1 _.set(xLens, 4, {
+ 5 other calls in file
GitHub: moyataka/amberik-core
76 77 78 79 80 81 82 83 84
const processContexts = makeProcessContexts(resolved_ctx) return pipe( map(({event, response}) => { const memory_key = R.view(memoryKeyLens)(event) const fallback_ctxs = selector.response.getContexts(memory_key)(response) const prev_state_ctxs = selector.event.getContexts(memory_key)(event) const res_ctxs = processContexts(prev_state_ctxs, fallback_ctxs)
198 199 200 201 202 203 204 205 206 207
(landscapeVideoHeight, parametricLadder) => rungSpecsReplace( RS.insertIntoArray( { dim: landscapeVideoHeight, bitrate: RS.bitrateForNoUpscaleRungSpec( R.view(RUNG_SPECS_LENS, parametricLadder), landscapeVideoHeight ) } ),
+ 505 other calls in file
GitHub: octachrome/t2
139 140 141 142 143 144 145 146 147 148
R.over(activePlayerLens, Player.adjustCash(toSteal)) )(context); }), playerHasCash: (idx, cash) => R.pipe( R.view(Player.lens(idx)), R.propEq('cash', cash) ), playerCanAffordAction: R.curry((idx, action, context) => {
+ 41 other calls in file
5004 5005 5006 5007 5008 5009 5010 5011 5012 5013
* @see R.prop, R.lensIndex, R.lensProp * @example * * var xLens = R.lensProp('x'); * * R.view(xLens, {x: 1, y: 2}); //=> 1 * R.view(xLens, {x: 4, y: 2}); //=> 4 */ // `Const` is a functor that effectively ignores the function given to `map`. // Using `Const` effectively ignores the setter function of the `lens`,
+ 107 other calls in file
GitHub: jcla1/AdventOfCode
33 34 35 36 37 38 39 40 41 42 43 44
return [parseInt(num), rest]; } }; const isSimpleNum = (n) => typeof(n) === 'number'; const view = (p, n) => R.view(R.lensPath(p), n); // eslint-disable-next-line no-unused-vars const snailNumToString = (n) => { if (isSimpleNum(n)) return R.toString(n);
+ 3 other calls in file
GitHub: octachrome/t2
107 108 109 110 111 112 113 114 115 116
[State.eq(State.ACTION_RESPONSE), game => R.flatten([ R.map(Action.challenge, Game.opponents(game)), Action.allow(), ])], [State.eq(State.CHALLENGE_RESPONSE), R.pipe( R.view(activePlayerLens), R.prop('influence'), R.map(R.prop('role')), R.map(Action.reveal) )],
+ 3 other calls in file
8824 8825 8826 8827 8828 8829 8830 8831 8832 8833
* @see R.view, R.set, R.over * @example * * const xHeadYLens = R.lensPath(['x', 0, 'y']); * * R.view(xHeadYLens, {x: [{y: 2, z: 3}, {y: 4, z: 5}]}); * //=> 2 * R.set(xHeadYLens, 1, {x: [{y: 2, z: 3}, {y: 4, z: 5}]}); * //=> {x: [{y: 1, z: 3}, {y: 4, z: 5}]} * R.over(xHeadYLens, R.negate, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
+ 23 other calls in file
ramda.clone is the most popular function in ramda (30311 examples)