-
Notifications
You must be signed in to change notification settings - Fork 4.7k
WordCount: Add types #22077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
WordCount: Add types #22077
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
437bbb3
Define WordCountStrategy
nb 43e6f52
TypeScript config for wordcount
nb b4a3cd8
Define a type for all the settings fields
nb 11a0305
Return unmodified text if regexp is missing
nb 57d3a1c
Use WordCountSettings type in all strip* functions
nb 1fa3545
No need to bind with current this
nb 17a717f
Switch helper functions to returning a number
nb 7d1b5ff
Update autogenerated docs
nb cb4e616
Add descriptions for WordCountSettingsFields
nb 17fac4c
Switch to single quotes
nb 768d1b5
Inline import @typedef to single line
nb 1b51511
Switch to switch
nb 15a735e
Prefix custom types with WP
nb 50dbe32
Remove unneeded {Object}
nb b2c26d8
Align comments
nb 46b211d
Introduce a new, narrower, settings type
nb 2dc5c11
Based on previous commit, relax undefined checks
nb dfaaff4
Move eslint-disable rule to correct place
nb 8d52e9c
Don't mutate default settings object when extending it
nb 983b4d4
No need for fallback for required and validate arg
nb e7811eb
Fix countWords return type label
ockham 1283f95
Fix some JSDoc whitespace
ockham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,18 +17,29 @@ import stripShortcodes from './stripShortcodes'; | |
| import stripSpaces from './stripSpaces'; | ||
| import transposeHTMLEntitiesToCountableChars from './transposeHTMLEntitiesToCountableChars'; | ||
|
|
||
| /** | ||
| * @typedef {import('./defaultSettings').WPWordCountDefaultSettings} WPWordCountSettings | ||
| * @typedef {import('./defaultSettings').WPWordCountUserSettings} WPWordCountUserSettings | ||
| */ | ||
|
|
||
| /** | ||
| * Possible ways of counting. | ||
| * | ||
| * @typedef {'words'|'characters_excluding_spaces'|'characters_including_spaces'} WPWordCountStrategy | ||
| */ | ||
|
|
||
| /** | ||
| * Private function to manage the settings. | ||
| * | ||
| * @param {string} type The type of count to be done. | ||
| * @param {Object} userSettings Custom settings for the count. | ||
| * @param {WPWordCountStrategy} type The type of count to be done. | ||
| * @param {WPWordCountUserSettings} userSettings Custom settings for the count. | ||
| * | ||
| * @return {void|Object|*} The combined settings object to be used. | ||
| * @return {WPWordCountSettings} The combined settings object to be used. | ||
nb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| function loadSettings( type, userSettings ) { | ||
| const settings = extend( defaultSettings, userSettings ); | ||
| const settings = extend( {}, defaultSettings, userSettings ); | ||
|
|
||
| settings.shortcodes = settings.l10n.shortcodes || {}; | ||
| settings.shortcodes = settings.l10n?.shortcodes ?? []; | ||
aduth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if ( settings.shortcodes && settings.shortcodes.length ) { | ||
| settings.shortcodesRegExp = new RegExp( | ||
|
|
@@ -37,7 +48,7 @@ function loadSettings( type, userSettings ) { | |
| ); | ||
| } | ||
|
|
||
| settings.type = type || settings.l10n.type; | ||
| settings.type = type; | ||
|
|
||
| if ( | ||
| settings.type !== 'characters_excluding_spaces' && | ||
|
|
@@ -50,56 +61,56 @@ function loadSettings( type, userSettings ) { | |
| } | ||
|
|
||
| /** | ||
| * Match the regex for the type 'words' | ||
| * Count the words in text | ||
| * | ||
| * @param {string} text The text being processed | ||
| * @param {string} regex The regular expression pattern being matched | ||
| * @param {Object} settings Settings object containing regular expressions for each strip function | ||
| * @param {string} text The text being processed | ||
| * @param {RegExp} regex The regular expression pattern being matched | ||
| * @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function | ||
| * | ||
| * @return {Array|{index: number, input: string}} The matched string. | ||
| * @return {number} Count of words. | ||
| */ | ||
| function matchWords( text, regex, settings ) { | ||
| function countWords( text, regex, settings ) { | ||
| text = flow( | ||
| stripTags.bind( this, settings ), | ||
| stripHTMLComments.bind( this, settings ), | ||
| stripShortcodes.bind( this, settings ), | ||
| stripSpaces.bind( this, settings ), | ||
| stripHTMLEntities.bind( this, settings ), | ||
| stripConnectors.bind( this, settings ), | ||
| stripRemovables.bind( this, settings ) | ||
| stripTags.bind( null, settings ), | ||
| stripHTMLComments.bind( null, settings ), | ||
| stripShortcodes.bind( null, settings ), | ||
| stripSpaces.bind( null, settings ), | ||
| stripHTMLEntities.bind( null, settings ), | ||
| stripConnectors.bind( null, settings ), | ||
| stripRemovables.bind( null, settings ) | ||
| )( text ); | ||
| text = text + '\n'; | ||
| return text.match( regex ); | ||
| return text.match( regex )?.length ?? 0; | ||
| } | ||
|
|
||
| /** | ||
| * Match the regex for either 'characters_excluding_spaces' or 'characters_including_spaces' | ||
| * Count the characters in text | ||
| * | ||
| * @param {string} text The text being processed | ||
| * @param {string} regex The regular expression pattern being matched | ||
| * @param {Object} settings Settings object containing regular expressions for each strip function | ||
| * @param {string} text The text being processed | ||
| * @param {RegExp} regex The regular expression pattern being matched | ||
| * @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function | ||
| * | ||
| * @return {Array|{index: number, input: string}} The matched string. | ||
| * @return {number} Count of characters. | ||
| */ | ||
| function matchCharacters( text, regex, settings ) { | ||
| function countCharacters( text, regex, settings ) { | ||
| text = flow( | ||
| stripTags.bind( this, settings ), | ||
| stripHTMLComments.bind( this, settings ), | ||
| stripShortcodes.bind( this, settings ), | ||
| stripSpaces.bind( this, settings ), | ||
| transposeAstralsToCountableChar.bind( this, settings ), | ||
| transposeHTMLEntitiesToCountableChars.bind( this, settings ) | ||
| stripTags.bind( null, settings ), | ||
| stripHTMLComments.bind( null, settings ), | ||
| stripShortcodes.bind( null, settings ), | ||
| transposeAstralsToCountableChar.bind( null, settings ), | ||
| stripSpaces.bind( null, settings ), | ||
| transposeHTMLEntitiesToCountableChars.bind( null, settings ) | ||
| )( text ); | ||
| text = text + '\n'; | ||
| return text.match( regex ); | ||
| return text.match( regex )?.length ?? 0; | ||
nb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Count some words. | ||
| * | ||
| * @param {string} text The text being processed | ||
| * @param {string} type The type of count. Accepts ;words', 'characters_excluding_spaces', or 'characters_including_spaces'. | ||
| * @param {Object} userSettings Custom settings object. | ||
| * @param {string} text The text being processed | ||
| * @param {WPWordCountStrategy} type The type of count. Accepts 'words', 'characters_excluding_spaces', or 'characters_including_spaces'. | ||
| * @param {WPWordCountUserSettings} userSettings Custom settings object. | ||
| * | ||
| * @example | ||
| * ```js | ||
|
|
@@ -109,20 +120,20 @@ function matchCharacters( text, regex, settings ) { | |
| * | ||
| * @return {number} The word or character count. | ||
| */ | ||
|
|
||
| export function count( text, type, userSettings ) { | ||
| if ( '' === text ) { | ||
| return 0; | ||
| } | ||
|
|
||
| if ( text ) { | ||
| const settings = loadSettings( type, userSettings ); | ||
| const matchRegExp = settings[ type + 'RegExp' ]; | ||
| const results = | ||
| 'words' === settings.type | ||
| ? matchWords( text, matchRegExp, settings ) | ||
| : matchCharacters( text, matchRegExp, settings ); | ||
|
|
||
| return results ? results.length : 0; | ||
| const settings = loadSettings( type, userSettings ); | ||
| let matchRegExp; | ||
| switch ( settings.type ) { | ||
| case 'words': | ||
| matchRegExp = settings.wordsRegExp; | ||
| return countWords( text, matchRegExp, settings ); | ||
| case 'characters_including_spaces': | ||
| matchRegExp = settings.characters_including_spacesRegExp; | ||
| return countCharacters( text, matchRegExp, settings ); | ||
| case 'characters_excluding_spaces': | ||
| matchRegExp = settings.characters_excluding_spacesRegExp; | ||
| return countCharacters( text, matchRegExp, settings ); | ||
| default: | ||
| return 0; | ||
|
Comment on lines
+126
to
+137
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like how this section turned out. It's nice that the |
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,11 @@ | ||
| /** | ||
| * Replaces items matched in the regex with spaces. | ||
| * | ||
| * @param {Object} settings The main settings object containing regular expressions | ||
| * @param {string} text The string being counted. | ||
| * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions | ||
| * @param {string} text The string being counted. | ||
| * | ||
| * @return {string} The manipulated text. | ||
| */ | ||
| export default function stripConnectors( settings, text ) { | ||
| if ( settings.connectorRegExp ) { | ||
| return text.replace( settings.connectorRegExp, ' ' ); | ||
| } | ||
| return text; | ||
ockham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return text.replace( settings.connectorRegExp, ' ' ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,11 @@ | ||
| /** | ||
| * Removes items matched in the regex. | ||
| * | ||
| * @param {Object} settings The main settings object containing regular expressions | ||
| * @param {string} text The string being counted. | ||
| * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions | ||
| * @param {string} text The string being counted. | ||
| * | ||
| * @return {string} The manipulated text. | ||
| */ | ||
| export default function stripHTMLComments( settings, text ) { | ||
| if ( settings.HTMLcommentRegExp ) { | ||
| return text.replace( settings.HTMLcommentRegExp, '' ); | ||
| } | ||
| return text; | ||
| return text.replace( settings.HTMLcommentRegExp, '' ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,11 @@ | ||
| /** | ||
| * Removes items matched in the regex. | ||
| * | ||
| * @param {Object} settings The main settings object containing regular expressions | ||
| * @param {string} text The string being counted. | ||
| * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions | ||
| * @param {string} text The string being counted. | ||
| * | ||
| * @return {string} The manipulated text. | ||
| */ | ||
| export default function stripHTMLEntities( settings, text ) { | ||
| if ( settings.HTMLEntityRegExp ) { | ||
| return text.replace( settings.HTMLEntityRegExp, '' ); | ||
| } | ||
| return text; | ||
| return text.replace( settings.HTMLEntityRegExp, '' ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,11 @@ | ||
| /** | ||
| * Removes items matched in the regex. | ||
| * | ||
| * @param {Object} settings The main settings object containing regular expressions | ||
| * @param {string} text The string being counted. | ||
| * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions | ||
| * @param {string} text The string being counted. | ||
| * | ||
| * @return {string} The manipulated text. | ||
| */ | ||
| export default function stripRemovables( settings, text ) { | ||
| if ( settings.removeRegExp ) { | ||
| return text.replace( settings.removeRegExp, '' ); | ||
| } | ||
| return text; | ||
| return text.replace( settings.removeRegExp, '' ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,11 @@ | ||
| /** | ||
| * Replaces items matched in the regex with spaces. | ||
| * | ||
| * @param {Object} settings The main settings object containing regular expressions | ||
| * @param {string} text The string being counted. | ||
| * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions | ||
| * @param {string} text The string being counted. | ||
| * | ||
| * @return {string} The manipulated text. | ||
| */ | ||
| export default function stripSpaces( settings, text ) { | ||
| if ( settings.spaceRegExp ) { | ||
| return text.replace( settings.spaceRegExp, ' ' ); | ||
| } | ||
| return text.replace( settings.spaceRegExp, ' ' ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,11 @@ | ||
| /** | ||
| * Replaces items matched in the regex with new line | ||
| * | ||
| * @param {Object} settings The main settings object containing regular expressions | ||
| * @param {string} text The string being counted. | ||
| * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions | ||
| * @param {string} text The string being counted. | ||
| * | ||
| * @return {string} The manipulated text. | ||
| */ | ||
| export default function stripTags( settings, text ) { | ||
| if ( settings.HTMLRegExp ) { | ||
| return text.replace( settings.HTMLRegExp, '\n' ); | ||
| } | ||
| return text.replace( settings.HTMLRegExp, '\n' ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,11 @@ | ||
| /** | ||
| * Replaces items matched in the regex with character. | ||
| * | ||
| * @param {Object} settings The main settings object containing regular expressions | ||
| * @param {string} text The string being counted. | ||
| * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions | ||
| * @param {string} text The string being counted. | ||
| * | ||
| * @return {string} The manipulated text. | ||
| */ | ||
| export default function transposeAstralsToCountableChar( settings, text ) { | ||
| if ( settings.astralRegExp ) { | ||
| return text.replace( settings.astralRegExp, 'a' ); | ||
| } | ||
| return text; | ||
| return text.replace( settings.astralRegExp, 'a' ); | ||
| } |
9 changes: 3 additions & 6 deletions
9
packages/wordcount/src/transposeHTMLEntitiesToCountableChars.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,14 @@ | ||
| /** | ||
| * Replaces items matched in the regex with a single character. | ||
| * | ||
| * @param {Object} settings The main settings object containing regular expressions | ||
| * @param {string} text The string being counted. | ||
| * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions | ||
| * @param {string} text The string being counted. | ||
| * | ||
| * @return {string} The manipulated text. | ||
| */ | ||
| export default function transposeHTMLEntitiesToCountableChars( | ||
| settings, | ||
| text | ||
| ) { | ||
| if ( settings.HTMLEntityRegExp ) { | ||
| return text.replace( settings.HTMLEntityRegExp, 'a' ); | ||
| } | ||
| return text; | ||
| return text.replace( settings.HTMLEntityRegExp, 'a' ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "rootDir": "src", | ||
| "declarationDir": "build-types" | ||
| }, | ||
| "include": [ "src/**/*" ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.