Zero-dependency Japanese quantity string parser for recipe ingredients
npm install @recipe-scope/quantity-parserbash
npm install @recipe-scope/quantity-parser
`
Usage
$3
`typescript
import { QuantityParser } from '@recipe-scope/quantity-parser';
// Simple quantities
QuantityParser.parse('100g'); // { value: 100, unit: 'g' }
QuantityParser.parse('大さじ1'); // { value: 1, unit: '大さじ' }
QuantityParser.parse('1/2カップ'); // { value: 0.5, unit: 'カップ' }
// Full-width numbers
QuantityParser.parse('3個'); // { value: 3, unit: '個' }
// Fractions
QuantityParser.parse('1と1/2カップ'); // { value: 1.5, unit: 'カップ' }
QuantityParser.parse('半個'); // { value: 0.5, unit: '個' }
// Ranges (uses minimum value)
QuantityParser.parse('1~2本'); // { value: 1, unit: '本' }
// Parentheses (extracts grams/ml)
QuantityParser.parse('なす(100g)'); // { value: 100, unit: 'g' }
// Tube measurements
QuantityParser.parse('チューブ3cm'); // { value: 3, unit: 'チューブcm' }
`
$3
`typescript
import { QuantityFormatter } from '@recipe-scope/quantity-parser';
// Basic formatting
QuantityFormatter.format(100, 'g'); // '100 g'
QuantityFormatter.format(2, '個'); // '2 個'
// Spoon fractions
QuantityFormatter.format(1.5, '大さじ'); // '大さじ 1と1/2'
QuantityFormatter.format(0.5, '小さじ'); // '小さじ 1/2'
// Tube measurements
QuantityFormatter.format(3, 'チューブcm'); // 'チューブ3cm'
// Size prefixes
QuantityFormatter.format(2, '小個'); // '小2個'
// Ambiguous units
QuantityFormatter.format(0, '少々'); // '少々'
QuantityFormatter.format(0, '適量'); // '適量'
`
$3
Access unit configuration for custom calculations:
`typescript
import { UNITS_CONFIG } from '@recipe-scope/quantity-parser';
// Standard volumes in ml
UNITS_CONFIG.STANDARD_VOLUME_ML['大さじ']; // 15
UNITS_CONFIG.STANDARD_VOLUME_ML['小さじ']; // 5
UNITS_CONFIG.STANDARD_VOLUME_ML['カップ']; // 200
// Countable unit aliases
UNITS_CONFIG.UNIT_SINGLE_ALIASES; // ['個', '本', '匹', '尾', '玉', '粒', '株']
// Ambiguous units
UNITS_CONFIG.AMBIGUOUS; // ['少々', '適量', 'ひとつまみ', '適宜', 'ひとつかみ']
``