Fhope Cc
Fhope Cc

Reputation: 45

Howto detect wide characters in javascript?

I write a small parser for a custom query language which contains Chinese characters. When detecting syntax error, it outputs error message as following:

語法錯誤:應為數,但為字串。
索引 = '3213茂訊'"
       ^

The last line has only one '^' character to indicate the position of error token. For Chinese characters' visual length occupy two other characters, I need to detect wide character to calculate the '^' position to indicate the right token. Does anyone knows some function can detect the wide character in javascript?

Upvotes: 0

Views: 307

Answers (1)

Leif Marcus
Leif Marcus

Reputation: 623

I’m not sure if I understand you correct. But you might want to try the https://www.npmjs.com/package/wcwidth package. Which can be implemented as follows:

import wcwidth from 'wcwidth';

const getCharAtPosition = (str, position) => {
  let currPos = 0;
  return [...str].find(char => {
    const charWidth = wcwidth(char);
    const isPosition =
      currPos === position || (charWidth === 2 && currPos === position - 1);
    currPos += charWidth;
    return isPosition;
  });
};

const indicatorPos = '       ^'.indexOf('^');
console.log(getCharAtPosition(`索引 = '3213茂訊'"`, indicatorPos));
// will log: '

I didn’t test it, but something like this might work.

Upvotes: 1

Related Questions