Skip to content

Latest commit

 

History

History
179 lines (148 loc) · 4.52 KB

RECIPES.md

File metadata and controls

179 lines (148 loc) · 4.52 KB

📚 Recipes

Demo

A collection of code snippets to make creating custom validators easy. These recipes could be supplied to create custom validators. Kana, Kanji and Japanese character validators are available out-of-the-box. Check out README.md for more details.

Template

Copy/paste this template and replace "options" with your choice of set from below

import {
  // Types
  UnicodeRange,
  CreateValidatorOptions,
  StrictValidator,
  ThresholdBasedValidator,
  MatchScoreCalculator,
  CharacterSet,

  // Functions
  createStrictValidator,
  createThresholdBasedValidator,
  createMatchScoreCalculator,
} from 'japanese-moji';

const options: CreateValidatorOptions = {
  // Required property
  characterSets: [
    // Mix/Match character sets here
  ],
  // Optional properties
  // customRanges?: UnicodeRange[];
  // customUnicodes?: string[];
};

const isValidCustom: StrictValidator = createStrictValidator(options);
const isCustomPresent: ThresholdBasedValidator = createThresholdBasedValidator(options);
const howMuchIsCustomPresent: MatchScoreCalculator = createMatchScoreCalculator(options);

Hiragana

Demo
const options: CreateValidatorOptions = {
  characterSets: [CharacterSet.Hiragana],
};

Hiragana + CJK Symbols & Punctuations

Demo
const options: CreateValidatorOptions = {
  characterSets: [CharacterSet.Hiragana, CharacterSet.CJKPunctuations],
};

Hiragana + All Katakana

Demo
const options: CreateValidatorOptions = {
  characterSets: [
    CharacterSet.Hiragana,
    CharacterSet.Katakana,
    CharacterSet.KatakanaPhoneticExtension,
    CharacterSet.HalfWidthKatakana,
  ],
};

Hiragana + All Kanji

Demo
const options: CreateValidatorOptions = {
  characterSets: [
    CharacterSet.Hiragana,
    CharacterSet.RareKanji,
    CharacterSet.CommonUncommonKanji,
    CharacterSet.KanjiCompatibilityIdeographs,
  ],
};

All fullwidth letters

Demo
const options: CreateValidatorOptions = {
  characterSets: [
    CharacterSet.FullWidthUpperCase,
    CharacterSet.FullWidthLowerCase,
    CharacterSet.FullWidthNumbers,
    CharacterSet.FullWidthPunctuations,
  ],
};

Latin punctuations + All Japanese

Demo
// includes: " " (space), !"#$%&'()*+,-./
const LatinPunctuations: UnicodeRange = {
  start: '\\u0020',
  end: '\\u002f',
};

// includes: :;<=>?@
const LatinPunctuationsExtensionA: UnicodeRange = {
  start: '\\u003a',
  end: '\\u0040',
};

// includes: [\]^_`
const LatinPunctuationsExtensionB: UnicodeRange = {
  start: '\\u005b',
  end: '\\u0060',
};

// includes: {|}~
const LatinPunctuationsExtensionC: UnicodeRange = {
  start: '\\u007b',
  end: '\\u007e',
};

const LatinPunctuationsRanges: UnicodeRange[] = [
  LatinPunctuations,
  LatinPunctuationsExtensionA,
  LatinPunctuationsExtensionB,
  LatinPunctuationsExtensionC,
];

const options: CreateValidatorOptions = {
  characterSets: [
    CharacterSet.CJKPunctuations,
    CharacterSet.Hiragana,
    CharacterSet.Katakana,
    CharacterSet.KatakanaPhoneticExtension,
    CharacterSet.RareKanji,
    CharacterSet.CommonUncommonKanji,
    CharacterSet.KanjiCompatibilityIdeographs,
    CharacterSet.HalfWidthKatakana,
    CharacterSet.FullWidthUpperCase,
    CharacterSet.FullWidthLowerCase,
    CharacterSet.FullWidthNumbers,
    CharacterSet.FullWidthPunctuations,
  ],
  customRanges: [...LatinPunctuationsRanges],
};