osrm-text-instructions
Advanced tools
Comparing version 0.0.7 to 0.1.0
{ | ||
"env": { | ||
"browser": true, | ||
"node": true | ||
"node": true, | ||
"es6": true | ||
}, | ||
@@ -9,3 +10,3 @@ "extends": "eslint:recommended", | ||
"accessor-pairs": "error", | ||
"array-bracket-spacing": "error", | ||
"array-bracket-spacing": "off", | ||
"array-callback-return": "error", | ||
@@ -126,3 +127,3 @@ "arrow-body-style": "error", | ||
"no-mixed-requires": "error", | ||
"no-multi-spaces": "error", | ||
"no-multi-spaces": "off", | ||
"no-multi-str": "error", | ||
@@ -129,0 +130,0 @@ "no-negated-condition": "error", |
@@ -8,2 +8,8 @@ # Change Log | ||
# 0.1.0 2016-11-17 | ||
- Improve chinese translation | ||
- Standardize capitalizeFirstLetter meta key | ||
- Change instructions object customization to options.hooks.tokenizedInstruction | ||
# 0.0.7 2016-11-10 | ||
@@ -10,0 +16,0 @@ |
56
index.js
@@ -1,9 +0,8 @@ | ||
module.exports = function(version, language) { | ||
module.exports = function(version, language, options) { | ||
// load instructions | ||
var instructions = require('./instructions').get(language); | ||
if (Object !== instructions.constructor) throw 'instructions must be object'; | ||
if (!instructions[version]) { throw 'invalid version ' + version; } | ||
var o = { | ||
instructions: instructions, | ||
return { | ||
capitalizeFirstLetter: function(string) { | ||
@@ -14,3 +13,3 @@ return string.charAt(0).toUpperCase() + string.slice(1); | ||
// Transform numbers to their translated ordinalized value | ||
return this.instructions[version].constants.ordinalize[number.toString()] || ''; | ||
return instructions[version].constants.ordinalize[number.toString()] || ''; | ||
}, | ||
@@ -23,19 +22,19 @@ directionFromDegree: function(degree) { | ||
} else if (degree >= 0 && degree <= 20) { | ||
return this.instructions[version].constants.direction.north; | ||
return instructions[version].constants.direction.north; | ||
} else if (degree > 20 && degree < 70) { | ||
return this.instructions[version].constants.direction.northeast; | ||
return instructions[version].constants.direction.northeast; | ||
} else if (degree >= 70 && degree < 110) { | ||
return this.instructions[version].constants.direction.east; | ||
return instructions[version].constants.direction.east; | ||
} else if (degree >= 110 && degree <= 160) { | ||
return this.instructions[version].constants.direction.southeast; | ||
return instructions[version].constants.direction.southeast; | ||
} else if (degree > 160 && degree <= 200) { | ||
return this.instructions[version].constants.direction.south; | ||
return instructions[version].constants.direction.south; | ||
} else if (degree > 200 && degree < 250) { | ||
return this.instructions[version].constants.direction.southwest; | ||
return instructions[version].constants.direction.southwest; | ||
} else if (degree >= 250 && degree <= 290) { | ||
return this.instructions[version].constants.direction.west; | ||
return instructions[version].constants.direction.west; | ||
} else if (degree > 290 && degree < 340) { | ||
return this.instructions[version].constants.direction.northwest; | ||
return instructions[version].constants.direction.northwest; | ||
} else if (degree >= 340 && degree <= 360) { | ||
return this.instructions[version].constants.direction.north; | ||
return instructions[version].constants.direction.north; | ||
} else { | ||
@@ -75,3 +74,3 @@ throw new Error('Degree ' + degree + ' invalid'); | ||
if (!this.instructions[version][type]) { | ||
if (!instructions[version][type]) { | ||
// Log for debugging | ||
@@ -85,10 +84,10 @@ console.log('Encountered unknown instruction type: ' + type); // eslint-disable-line no-console | ||
// Use special this.instructions if available, otherwise `defaultinstruction` | ||
// Use special instructions if available, otherwise `defaultinstruction` | ||
var instructionObject; | ||
if (this.instructions[version].modes[mode]) { | ||
instructionObject = this.instructions[version].modes[mode]; | ||
} else if (this.instructions[version][type][modifier]) { | ||
instructionObject = this.instructions[version][type][modifier]; | ||
if (instructions[version].modes[mode]) { | ||
instructionObject = instructions[version].modes[mode]; | ||
} else if (instructions[version][type][modifier]) { | ||
instructionObject = instructions[version][type][modifier]; | ||
} else { | ||
instructionObject = this.instructions[version][type].default; | ||
instructionObject = instructions[version][type].default; | ||
} | ||
@@ -100,7 +99,7 @@ | ||
case 'use lane': | ||
laneInstruction = this.instructions[version].constants.lanes[this.laneConfig(step)]; | ||
laneInstruction = instructions[version].constants.lanes[this.laneConfig(step)]; | ||
if (!laneInstruction) { | ||
// If the lane combination is not found, default to continue straight | ||
instructionObject = this.instructions[version]['use lane'].no_lanes; | ||
instructionObject = instructions[version]['use lane'].no_lanes; | ||
} | ||
@@ -156,2 +155,7 @@ break; | ||
var tokenizedInstructionHook = ((options || {}).hooks || {}).tokenizedInstruction; | ||
if (tokenizedInstructionHook) { | ||
instruction = tokenizedInstructionHook(instruction); | ||
} | ||
// Replace tokens | ||
@@ -166,3 +170,3 @@ // NOOP if they don't exist | ||
.replace('{lane_instruction}', laneInstruction) | ||
.replace('{modifier}', this.instructions[version].constants.modifier[modifier]) | ||
.replace('{modifier}', instructions[version].constants.modifier[modifier]) | ||
.replace('{direction}', this.directionFromDegree(step.maneuver.bearing_after)) | ||
@@ -172,3 +176,3 @@ .replace('{nth}', nthWaypoint) | ||
if (this.instructions.meta.capitalize_first_letter) { | ||
if (instructions.meta.capitalizeFirstLetter) { | ||
instruction = this.capitalizeFirstLetter(instruction); | ||
@@ -180,4 +184,2 @@ } | ||
}; | ||
return o; | ||
}; |
{ | ||
"meta": { | ||
"capitalize_first_letter": true | ||
"capitalizeFirstLetter": true | ||
}, | ||
@@ -5,0 +5,0 @@ "v5": { |
{ | ||
"meta": { | ||
"capitalizeFirstLetter": true | ||
"capitalizeFirstLetter": false | ||
}, | ||
@@ -34,4 +34,4 @@ "v5": { | ||
"sharp right": "向右", | ||
"slight left": "稍向左", | ||
"slight right": "稍向右", | ||
"slight left": "向左", | ||
"slight right": "向右", | ||
"straight": "直行", | ||
@@ -59,21 +59,21 @@ "uturn": "调头" | ||
"left": { | ||
"default": "您已经到达您的{nth}个目的地, 在道路左侧" | ||
"default": "您已经到达您的{nth}个目的地,在道路左侧" | ||
}, | ||
"right": { | ||
"default": "您已经到达您的{nth}个目的地, 在道路右侧" | ||
"default": "您已经到达您的{nth}个目的地,在道路右侧" | ||
}, | ||
"sharp left": { | ||
"default": "您已经到达您的{nth}个目的地, 在道路左侧" | ||
"default": "您已经到达您的{nth}个目的地,在道路左侧" | ||
}, | ||
"sharp right": { | ||
"default": "您已经到达您的{nth}个目的地, 在道路右侧" | ||
"default": "您已经到达您的{nth}个目的地,在道路右侧" | ||
}, | ||
"slight right": { | ||
"default": "您已经到达您的{nth}个目的地, 在道路右侧" | ||
"default": "您已经到达您的{nth}个目的地,在道路右侧" | ||
}, | ||
"slight left": { | ||
"default": "您已经到达您的{nth}个目的地, 在道路左侧" | ||
"default": "您已经到达您的{nth}个目的地,在道路左侧" | ||
}, | ||
"straight": { | ||
"default": "您已经到达您的{nth}个目的地, 在您正前方" | ||
"default": "您已经到达您的{nth}个目的地,在您正前方" | ||
} | ||
@@ -95,4 +95,4 @@ }, | ||
"default": { | ||
"default": "朝{direction}方向出发", | ||
"name": "朝{direction}方向上{way_name}" | ||
"default": "出发向{direction}", | ||
"name": "出发向{direction},上{way_name}" | ||
} | ||
@@ -144,4 +144,4 @@ }, | ||
"default": "继续{modifier}", | ||
"name": "继续{modifier}, 上{way_name}", | ||
"destination": "继续{modifier}, 前往{destination}" | ||
"name": "继续{modifier},上{way_name}", | ||
"destination": "继续{modifier},前往{destination}" | ||
}, | ||
@@ -168,3 +168,3 @@ "uturn": { | ||
"default": { | ||
"default": "通过匝道", | ||
"default": "上匝道", | ||
"name": "通过匝道驶入{way_name}", | ||
@@ -242,4 +242,4 @@ "destination": "通过匝道前往{destination}" | ||
"default": "在环岛{modifier}行驶", | ||
"name": "在环岛{modifier}行驶前往{way_name}", | ||
"destination": "At the roundabout make a {modifier} towards {destination}" | ||
"name": "在环岛{modifier}行驶,上{way_name}", | ||
"destination": "在环岛{modifier}行驶,前往{destination}" | ||
}, | ||
@@ -246,0 +246,0 @@ "left": { |
@@ -6,3 +6,3 @@ { | ||
"homepage": "http://project-osrm.org", | ||
"version": "0.0.7", | ||
"version": "0.1.0", | ||
"main": "./index.js", | ||
@@ -22,5 +22,6 @@ "license": "BSD-2-Clause", | ||
"scripts": { | ||
"lint": "eslint *.js", | ||
"test": "npm run lint && tape test/*_test.js" | ||
"lint": "eslint *.js test/*.js", | ||
"pretest": "npm run lint", | ||
"test": "tape test/*_test.js" | ||
} | ||
} |
@@ -21,3 +21,6 @@ # osrm-text-instructions | ||
``` | ||
var osrmTextInstructions = require('osrm-text-instructions')('v5', 'en'); | ||
var version = 'v5'; | ||
var language = 'en'; | ||
var options = {}; | ||
var osrmTextInstructions = require('osrm-text-instructions')(version, language, options); | ||
@@ -33,2 +36,8 @@ // make your request against the API | ||
parameter | required? | values | description | ||
---|----|----|--- | ||
`version` | required | `v5` | Major OSRM version | ||
`language` | required | `en` `de` `zh-Hans` `fr` `nl` | Language identifier | ||
`options.hooks.tokenizedIstruction` | optional | `function(instruction)` | A function to change the raw instruction string before tokens are replaced. Useful to inject custom markup for tokens | ||
### Development | ||
@@ -71,3 +80,3 @@ #### Architecture | ||
- `git checkout master` | ||
- Update Changelog.md | ||
- Update CHANGELOG.md | ||
- Bump version in package.json | ||
@@ -74,0 +83,0 @@ - `git commit -am "vx.y.z"` with Changelog list in commit message |
@@ -50,3 +50,3 @@ var path = require('path'); | ||
types.forEach(function(type) { | ||
switch(type) { | ||
switch (type) { | ||
case 'other': | ||
@@ -71,3 +71,3 @@ [ | ||
case 'rotary': | ||
[ 'default', 'exit_1', 'name', 'name_exit' ].forEach((s) => { | ||
['default', 'exit_1', 'name', 'name_exit'].forEach((s) => { | ||
assert.ok( | ||
@@ -85,10 +85,10 @@ fs.existsSync(path.join(basePath, 'rotary', `${s}_default.json`)), | ||
// special fixtures for ordinalization | ||
for (i = 2; i <= 11; i++) { | ||
for (var i = 2; i <= 11; i += 1) { | ||
assert.ok( | ||
fs.existsSync(path.join(basePath, 'rotary', `exit_${i}.json`)), | ||
`${type}/exit_${i}_default`); | ||
}; | ||
} | ||
break; | ||
case 'roundabout': | ||
[ 'default', 'exit' ].forEach((s) => { | ||
['default', 'exit'].forEach((s) => { | ||
assert.ok( | ||
@@ -128,3 +128,3 @@ fs.existsSync(path.join(basePath, 'roundabout', `${s}_default.json`)), | ||
break; | ||
}; | ||
} | ||
}); | ||
@@ -131,0 +131,0 @@ |
@@ -6,5 +6,6 @@ { | ||
"type": "arrive" | ||
} | ||
}, | ||
"name": "" | ||
}, | ||
"instruction": "You have arrived at your destination" | ||
} |
@@ -6,5 +6,6 @@ { | ||
"bearing_after": 0 | ||
} | ||
}, | ||
"name": "" | ||
}, | ||
"instruction": "Head north" | ||
} |
@@ -10,3 +10,3 @@ var path = require('path'); | ||
assert.equal( | ||
v5Instructions.directionFromDegree(undefined), | ||
v5Instructions.directionFromDegree(undefined), // eslint-disable-line no-undefined | ||
'', | ||
@@ -45,3 +45,3 @@ 'empty string for undefined' | ||
assert.throws( | ||
() => { v5Instructions.directionFromDegree(361) }, | ||
() => { v5Instructions.directionFromDegree(361); }, | ||
'throws on out of bounds degree' | ||
@@ -60,7 +60,7 @@ ); | ||
{ | ||
lanes: config.map((v) => { return { "valid": v }}) | ||
lanes: config.map((v) => ({'valid': v})) | ||
} | ||
] | ||
}; | ||
}; | ||
} | ||
@@ -85,3 +85,9 @@ [ | ||
assert.throws( | ||
() => { v5Instructions.laneConfig({ intersections: [ {} ] }); }, | ||
() => { | ||
v5Instructions.laneConfig({ | ||
intersections: [ | ||
{} | ||
] | ||
}); | ||
}, | ||
'throws on non-existing lanes' | ||
@@ -122,2 +128,21 @@ ); | ||
}); | ||
t.test('respects options.instructionStringHook', function(assert) { | ||
var v5Instructions = instructions('v5', 'en', { | ||
hooks: { | ||
tokenizedInstruction: function(instruction) { | ||
return instruction.replace('{way_name}', '<blink>{way_name}</blink>'); | ||
} | ||
} | ||
}); | ||
assert.equal(v5Instructions.compile({ | ||
"maneuver": { | ||
"type": "turn", | ||
"modifier": "left" | ||
}, | ||
"name": "Way Name" | ||
}), 'Turn left onto <blink>Way Name</blink>'); | ||
assert.end(); | ||
}); | ||
}); |
@@ -44,3 +44,3 @@ var tape = require('tape'); | ||
assert.throws( | ||
() => { instructions('v5', 'this-will-never-exist') }, | ||
() => { instructions('v5', 'this-will-never-exist'); }, | ||
'throws on non-existing language' | ||
@@ -47,0 +47,0 @@ ); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
198712
6398
84