index.js 814 B

12345678910111213141516171819202122232425262728293031
  1. import {getCategory, isAmbiguous, isFullWidth, isWide} from './lookup.js';
  2. function validate(codePoint) {
  3. if (!Number.isSafeInteger(codePoint)) {
  4. throw new TypeError(`Expected a code point, got \`${typeof codePoint}\`.`);
  5. }
  6. }
  7. export function eastAsianWidthType(codePoint) {
  8. validate(codePoint);
  9. return getCategory(codePoint);
  10. }
  11. export function eastAsianWidth(codePoint, {ambiguousAsWide = false} = {}) {
  12. validate(codePoint);
  13. if (
  14. isFullWidth(codePoint)
  15. || isWide(codePoint)
  16. || (ambiguousAsWide && isAmbiguous(codePoint))
  17. ) {
  18. return 2;
  19. }
  20. return 1;
  21. }
  22. // For Prettier. This doesn't count "ambiguous" characters or check for valid input.
  23. // https://github.com/sindresorhus/get-east-asian-width/pull/6
  24. export const _isNarrowWidth = codePoint => !(isFullWidth(codePoint) || isWide(codePoint));