@wordpress/wordcount
Version:
WordPress word count utility.
8 lines (7 loc) • 6.22 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../src/index.ts"],
"sourcesContent": ["/**\n * Internal dependencies\n */\nimport { defaultSettings } from './defaultSettings';\nimport stripTags from './stripTags';\nimport transposeAstralsToCountableChar from './transposeAstralsToCountableChar';\nimport stripHTMLEntities from './stripHTMLEntities';\nimport stripConnectors from './stripConnectors';\nimport stripRemovables from './stripRemovables';\nimport stripHTMLComments from './stripHTMLComments';\nimport stripShortcodes from './stripShortcodes';\nimport stripSpaces from './stripSpaces';\nimport transposeHTMLEntitiesToCountableChars from './transposeHTMLEntitiesToCountableChars';\n\nimport type { Settings, UserSettings, Strategy } from './types';\n\n/**\n * Private function to manage the settings.\n *\n * @param type The type of count to be done.\n * @param userSettings Custom settings for the count.\n * @return The combined settings object to be used.\n */\nfunction loadSettings(\n\ttype: Strategy = 'words',\n\tuserSettings: UserSettings = {}\n): Settings {\n\tconst mergedSettings = { ...defaultSettings, ...userSettings };\n\n\tconst settings: Settings = {\n\t\t...mergedSettings,\n\t\ttype,\n\t\tshortcodes: [],\n\t};\n\n\tsettings.shortcodes = settings.l10n?.shortcodes ?? [];\n\n\tif ( settings.shortcodes && settings.shortcodes.length ) {\n\t\tsettings.shortcodesRegExp = new RegExp(\n\t\t\t'\\\\[\\\\/?(?:' + settings.shortcodes.join( '|' ) + ')[^\\\\]]*?\\\\]',\n\t\t\t'g'\n\t\t);\n\t}\n\n\tif (\n\t\tsettings.type !== 'characters_excluding_spaces' &&\n\t\tsettings.type !== 'characters_including_spaces'\n\t) {\n\t\tsettings.type = 'words';\n\t}\n\n\treturn settings;\n}\n\n/**\n * Count the words in text\n *\n * @param text The text being processed\n * @param regex The regular expression pattern being matched\n * @param settings Settings object containing regular expressions for each strip function\n * @return Count of words.\n */\nfunction countWords( text: string, regex: RegExp, settings: Settings ): number {\n\ttext = [\n\t\tstripTags.bind( null, settings ),\n\t\tstripHTMLComments.bind( null, settings ),\n\t\tstripShortcodes.bind( null, settings ),\n\t\tstripSpaces.bind( null, settings ),\n\t\tstripHTMLEntities.bind( null, settings ),\n\t\tstripConnectors.bind( null, settings ),\n\t\tstripRemovables.bind( null, settings ),\n\t].reduce( ( result, fn ) => fn( result ), text );\n\ttext = text + '\\n';\n\treturn text.match( regex )?.length ?? 0;\n}\n\n/**\n * Count the characters in text\n *\n * @param text The text being processed\n * @param regex The regular expression pattern being matched\n * @param settings Settings object containing regular expressions for each strip function\n * @return Count of characters.\n */\nfunction countCharacters(\n\ttext: string,\n\tregex: RegExp,\n\tsettings: Settings\n): number {\n\ttext = [\n\t\tstripTags.bind( null, settings ),\n\t\tstripHTMLComments.bind( null, settings ),\n\t\tstripShortcodes.bind( null, settings ),\n\t\ttransposeAstralsToCountableChar.bind( null, settings ),\n\t\tstripSpaces.bind( null, settings ),\n\t\ttransposeHTMLEntitiesToCountableChars.bind( null, settings ),\n\t].reduce( ( result, fn ) => fn( result ), text );\n\ttext = text + '\\n';\n\treturn text.match( regex )?.length ?? 0;\n}\n\n/**\n * Count some words.\n *\n * @param text The text being processed\n * @param type The type of count. Accepts 'words', 'characters_excluding_spaces', or 'characters_including_spaces'.\n * @param userSettings Custom settings object.\n *\n * @example\n * ```ts\n * import { count } from '@wordpress/wordcount';\n * const numberOfWords = count( 'Words to count', 'words', {} )\n * ```\n *\n * @return The word or character count.\n */\nexport function count(\n\ttext: string,\n\ttype: Strategy,\n\tuserSettings?: UserSettings\n): number {\n\tconst settings = loadSettings( type, userSettings );\n\tlet matchRegExp: RegExp;\n\tswitch ( settings.type ) {\n\t\tcase 'words':\n\t\t\tmatchRegExp = settings.wordsRegExp;\n\t\t\treturn countWords( text, matchRegExp, settings );\n\t\tcase 'characters_including_spaces':\n\t\t\tmatchRegExp = settings.characters_including_spacesRegExp;\n\t\t\treturn countCharacters( text, matchRegExp, settings );\n\t\tcase 'characters_excluding_spaces':\n\t\t\tmatchRegExp = settings.characters_excluding_spacesRegExp;\n\t\t\treturn countCharacters( text, matchRegExp, settings );\n\t\tdefault:\n\t\t\treturn 0;\n\t}\n}\n\n// Export types for external usage\nexport type * from './types';\n"],
"mappings": ";AAGA,SAAS,uBAAuB;AAChC,OAAO,eAAe;AACtB,OAAO,qCAAqC;AAC5C,OAAO,uBAAuB;AAC9B,OAAO,qBAAqB;AAC5B,OAAO,qBAAqB;AAC5B,OAAO,uBAAuB;AAC9B,OAAO,qBAAqB;AAC5B,OAAO,iBAAiB;AACxB,OAAO,2CAA2C;AAWlD,SAAS,aACR,OAAiB,SACjB,eAA6B,CAAC,GACnB;AACX,QAAM,iBAAiB,EAAE,GAAG,iBAAiB,GAAG,aAAa;AAE7D,QAAM,WAAqB;AAAA,IAC1B,GAAG;AAAA,IACH;AAAA,IACA,YAAY,CAAC;AAAA,EACd;AAEA,WAAS,aAAa,SAAS,MAAM,cAAc,CAAC;AAEpD,MAAK,SAAS,cAAc,SAAS,WAAW,QAAS;AACxD,aAAS,mBAAmB,IAAI;AAAA,MAC/B,eAAe,SAAS,WAAW,KAAM,GAAI,IAAI;AAAA,MACjD;AAAA,IACD;AAAA,EACD;AAEA,MACC,SAAS,SAAS,iCAClB,SAAS,SAAS,+BACjB;AACD,aAAS,OAAO;AAAA,EACjB;AAEA,SAAO;AACR;AAUA,SAAS,WAAY,MAAc,OAAe,UAA6B;AAC9E,SAAO;AAAA,IACN,UAAU,KAAM,MAAM,QAAS;AAAA,IAC/B,kBAAkB,KAAM,MAAM,QAAS;AAAA,IACvC,gBAAgB,KAAM,MAAM,QAAS;AAAA,IACrC,YAAY,KAAM,MAAM,QAAS;AAAA,IACjC,kBAAkB,KAAM,MAAM,QAAS;AAAA,IACvC,gBAAgB,KAAM,MAAM,QAAS;AAAA,IACrC,gBAAgB,KAAM,MAAM,QAAS;AAAA,EACtC,EAAE,OAAQ,CAAE,QAAQ,OAAQ,GAAI,MAAO,GAAG,IAAK;AAC/C,SAAO,OAAO;AACd,SAAO,KAAK,MAAO,KAAM,GAAG,UAAU;A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