-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
139 lines (127 loc) · 4.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* External dependencies
*/
import { extend, flow } from 'lodash';
/**
* Internal dependencies
*/
import { defaultSettings } from './defaultSettings';
import stripTags from './stripTags';
import transposeAstralsToCountableChar from './transposeAstralsToCountableChar';
import stripHTMLEntities from './stripHTMLEntities';
import stripConnectors from './stripConnectors';
import stripRemovables from './stripRemovables';
import stripHTMLComments from './stripHTMLComments';
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 {WPWordCountStrategy} type The type of count to be done.
* @param {WPWordCountUserSettings} userSettings Custom settings for the count.
*
* @return {WPWordCountSettings} The combined settings object to be used.
*/
function loadSettings( type, userSettings ) {
const settings = extend( {}, defaultSettings, userSettings );
settings.shortcodes = settings.l10n?.shortcodes ?? [];
if ( settings.shortcodes && settings.shortcodes.length ) {
settings.shortcodesRegExp = new RegExp(
'\\[\\/?(?:' + settings.shortcodes.join( '|' ) + ')[^\\]]*?\\]',
'g'
);
}
settings.type = type;
if (
settings.type !== 'characters_excluding_spaces' &&
settings.type !== 'characters_including_spaces'
) {
settings.type = 'words';
}
return settings;
}
/**
* Count the words in text
*
* @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 {number} The matched string.
*/
function countWords( text, regex, settings ) {
text = flow(
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 )?.length ?? 0;
}
/**
* Count the characters in text
*
* @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 {number} Count of characters.
*/
function countCharacters( text, regex, settings ) {
text = flow(
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 )?.length ?? 0;
}
/**
* Count some words.
*
* @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
* import { count } from '@wordpress/wordcount';
* const numberOfWords = count( 'Words to count', 'words', {} )
* ```
*
* @return {number} The word or character count.
*/
export function count( text, type, userSettings ) {
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;
}
}