From 4d11f34e1deba1470e43271cdfc55bab28c0df8a Mon Sep 17 00:00:00 2001 From: Jannis R Date: Fri, 27 Dec 2019 21:22:01 +0100 Subject: [PATCH] adapt "writing a profile" guide to ctx-based parse fns :memo: [ci skip] --- docs/writing-a-profile.md | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/docs/writing-a-profile.md b/docs/writing-a-profile.md index cfacca40..fc0098c2 100644 --- a/docs/writing-a-profile.md +++ b/docs/writing-a-profile.md @@ -35,25 +35,33 @@ Assuming their HAFAS endpoint returns all line names prefixed with `foo `, we ca ```js // get the default line parser -const createParseLine = require('hafas-client/parse/line') +const parseLine = require('hafas-client/parse/line') -const createParseLineWithoutFoo = (profile, opt, data) => { - const parseLine = createParseLine(profile, opt, data) - - // wrapper function with additional logic - const parseLineWithoutFoo = (l) => { - const line = parseLine(l) - line.name = line.name.replace(/foo /g, '') - return line - } - return parseLineWithoutFoo +// wrapper function with additional logic +const parseLineWithoutFoo = (ctx, rawLine) => { + const line = parseLine(ctx, rawLine) + line.name = line.name.replace(/foo /g, '') + return line } -profile.parseLine = createParseLineWithoutFoo +myProfile.parseLine = parseLineWithoutFoo ``` If you pass this profile into `hafas-client`, the `parseLine` method will override [the default one](../parse/line.js). +You can also use the `parseHook` helper to reduce boilerplate: + +```js +const {parseHook} = require('hafas-client/lib/profile-hooks') + +const removeFoo = (ctx, rawLine) => ({ + ...ctx.parsed, + name: line.name.replace(/foo /g, '') +}) + +myProfile.parseLine = parseHook(parseLine, removeFoo) +``` + ## 1. Setup *Note*: There are many ways to find the required values. This way is rather easy and has worked for most of the apps that we've looked at so far.