Action

Unicode Fractions

Posted by garbonsai, Last update about 2 years ago

UPDATES

about 2 years ago

  • Edited description

Converts fractions to their unicode equivalents. I haven’t done a lot of testing against edge cases, but so far, so good.

Steps

  • script

    // do the actual replacing
    function replacer( match, cg1, cg2 ) {
    	return cg1.concat( fractions[ cg2 ] );
    }
    
    // regexp to match fractions that have unicode equivalents
    const needle = new RegExp(
    	/(^| |\t)((0\/3)|(1\/([2-9]|10))|(2\/[3,5])|(3\/[4,5,8])|(4\/5)|(5\/[6,8])|(7\/8))(?![0-9])/,
    	'gim'
    );
    
    // associative array of unicode fractions
    const fractions = {
    	'0/3': '↉',
    	'1/2': '½',
    	'1/3': '⅓',
    	'1/4': '¼',
    	'1/5': '⅕',
    	'1/6': '⅙',
    	'1/7': '⅐',
    	'1/8': '⅛',
    	'1/9': '⅑',
    	'1/10': '⅒',
    	'2/3': '⅔',
    	'2/5': '⅖',
    	'3/4': '¾',
    	'3/5': '⅗',
    	'3/8': '⅜',
    	'4/5': '⅘',
    	'5/6': '⅚',
    	'5/8': '⅝',
    	'7/8': '⅞'
    };
    
    // replace fractions with their unicode equivalent
    editor.setText( editor.getText().replace( needle, replacer ) );

Options

  • After Success Default
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.