{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { 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';\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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