From c039ce27666c293f660abfefdbd2de81e78a8e3d Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 12 Aug 2024 09:26:26 +0200 Subject: [PATCH] fix(tokenize): support reserved "constructor" token --- prototype/tokenize.js | 4 ++-- test/prototype/tokenize.js | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/prototype/tokenize.js b/prototype/tokenize.js index 9340e0fd..23fef497 100644 --- a/prototype/tokenize.js +++ b/prototype/tokenize.js @@ -84,11 +84,11 @@ function _groups(tokens, phrases) { // key is a single word token (the first word in // the phrase) and the values is an array of // phrases which contain that word. - const index = {}; + const index = Object.create(null); phrases.forEach( phrase => { const words = phrase.split(/\s+/); const firstWord = words[0]; - if( !index.hasOwnProperty( firstWord ) ){ + if( !index[ firstWord ] ){ index[ firstWord ] = []; } index[ firstWord ].push( words ); diff --git a/test/prototype/tokenize.js b/test/prototype/tokenize.js index cb5d4de2..e425509a 100644 --- a/test/prototype/tokenize.js +++ b/test/prototype/tokenize.js @@ -177,6 +177,17 @@ module.exports._groups = function(test, common) { t.deepEqual(tokenize._groups(tokens, phrases), expected); t.end(); }); + + // https://github.com/pelias/placeholder/issues/231 + test('_groups "constructor"', function(t) { + + const tokens = ['constructor']; + const phrases = []; + const expected = []; + + t.deepEqual(tokenize._groups(tokens, phrases), expected); + t.end(); + }); }; //