Skip to content

Instantly share code, notes, and snippets.

@andrewhosgood
Last active October 5, 2017 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewhosgood/feadaa62bb601eeb6fc4f3c4e6a3ee7f to your computer and use it in GitHub Desktop.
Save andrewhosgood/feadaa62bb601eeb6fc4f3c4e6a3ee7f to your computer and use it in GitHub Desktop.
Adapt Word Count
let WORDCOUNT = 0,
CHARACTERCOUNT = 0,
CHARACTERCOUNTWITHSPACES = 0;
const ADAPT = requirejs( 'coreJS/adapt' ),
COURSE = ADAPT.course,
CONTENTOBJECTS = ADAPT.contentObjects._byId,
BLOCKS = ADAPT.blocks._byId,
ARTICLES = ADAPT.articles._byId,
COMPONENTS = ADAPT.components._byId,
WHITELISTEDKEYS = ['title', 'displayTitle', 'body', 'instruction', 'mobileInstruction', 'text', 'alt', 'correct', 'final', 'front', 'strapline'],
BLACKLISTEDKEYS = ['_children'];
function stripHTML( rawHTML ) {
let tmp = document.createElement( 'div' );
tmp.innerHTML = rawHTML;
return tmp.textContent || tmp.innerText || '';
}
function walkThroughCountWords( object, path = 'ROOT' ) {
let count = 0;
for( let key in object ) {
if( object.hasOwnProperty( key ) &&
BLACKLISTEDKEYS.indexOf( key ) === -1 ) {
let value = object[key];
switch( typeof value ) {
case 'string':
if( key.substr( 0, 1 ) !== '_' ) {
count += stripHTML( value ).split( ' ' ).length;
}
break;
case 'object':
case 'array':
count += walkThroughCountWords( value, path + ' -> ' + key );
break;
default:
break;
}
}
}
return count;
}
function walkThroughCountCharacters( object, includeSpaces = false, path = 'ROOT' ) {
let count = 0;
for( let key in object ) {
if( object.hasOwnProperty( key ) &&
BLACKLISTEDKEYS.indexOf( key ) === -1 ) {
let value = object[key];
switch( typeof value ) {
case 'string':
if( key.substr( 0, 1 ) !== '_' ) {
let rawText = stripHTML( value );
if( includeSpaces === true ) {
count += rawText.length;
} else {
count += rawText.replace( /\s+/g, '' ).length;
}
}
break;
case 'object':
case 'array':
count += walkThroughCountWords( value, includeSpaces, path + ' -> ' + key );
break;
default:
break;
}
}
}
return count;
}
/**
* COURSE
*/
WORDCOUNT += walkThroughCountWords( COURSE.attributes );
CHARACTERCOUNT += walkThroughCountCharacters( COURSE.attributes );
CHARACTERCOUNTWITHSPACES += walkThroughCountCharacters( COURSE.attributes, true );
/**
* CONTENT OBJECTS
*/
for( let coID in CONTENTOBJECTS ) {
if( CONTENTOBJECTS.hasOwnProperty( coID ) ) {
let attributes = CONTENTOBJECTS[coID].attributes;
for( let attribute in attributes ) {
if( attributes.hasOwnProperty( attribute ) &&
WHITELISTEDKEYS.indexOf( attribute ) !== -1 ) {
let rawText = stripHTML( attributes[attribute] );
WORDCOUNT += rawText.split( ' ' ).length;
CHARACTERCOUNT += rawText.replace( /\s+/g, '' ).length;
CHARACTERCOUNTWITHSPACES += rawText.length;
}
}
}
}
/**
* ARTICLES
*/
for( let articleID in ARTICLES ) {
if( ARTICLES.hasOwnProperty( articleID ) ) {
let attributes = ARTICLES[articleID].attributes;
for( let attribute in attributes ) {
if( attributes.hasOwnProperty( attribute ) &&
WHITELISTEDKEYS.indexOf( attribute ) !== -1 ) {
let rawText = stripHTML( attributes[attribute] );
WORDCOUNT += rawText.split( ' ' ).length;
CHARACTERCOUNT += rawText.replace( /\s+/g, '' ).length;
CHARACTERCOUNTWITHSPACES += rawText.length;
}
}
}
}
/**
* BLOCKS
*/
for( let blockID in BLOCKS ) {
if( BLOCKS.hasOwnProperty( blockID ) ) {
let attributes = BLOCKS[blockID].attributes;
for( let attribute in attributes ) {
if( attributes.hasOwnProperty( attribute ) &&
WHITELISTEDKEYS.indexOf( attribute ) !== -1 ) {
let rawText = stripHTML( attributes[attribute] );
WORDCOUNT += rawText.split( ' ' ).length;
CHARACTERCOUNT += rawText.replace( /\s+/g, '' ).length;
CHARACTERCOUNTWITHSPACES += rawText.length;
}
}
}
}
/**
* COMPONENTS
*/
for( let componentID in COMPONENTS ) {
if( COMPONENTS.hasOwnProperty( componentID ) ) {
let attributes = COMPONENTS[componentID].attributes;
if( attributes ) {
for( let attribute in attributes ) {
if( attributes.hasOwnProperty( attribute ) &&
WHITELISTEDKEYS.indexOf( attribute ) !== -1 ) {
let rawText = stripHTML( attributes[attribute] );
WORDCOUNT += rawText.split( ' ' ).length;
CHARACTERCOUNT += rawText.replace( /\s+/g, '' ).length;
CHARACTERCOUNTWITHSPACES += rawText.length;
} else if( attribute === '_items' ) {
for( let itemNumber in attributes._items ) {
if( attributes._items.hasOwnProperty( itemNumber ) ) {
let item = attributes._items[itemNumber];
for( let itemAttribute in item ) {
if( item.hasOwnProperty( itemAttribute ) &&
WHITELISTEDKEYS.indexOf( itemAttribute ) !== -1 ) {
let rawText = stripHTML( item[itemAttribute] );
WORDCOUNT += rawText.split( ' ' ).length;
CHARACTERCOUNT += rawText.replace( /\s+/g, '' ).length;
CHARACTERCOUNTWITHSPACES += rawText.length;
}
}
}
}
}
}
}
}
}
console.log( '================================================' );
console.log( 'Words | ' + WORDCOUNT.toLocaleString() );
console.log( 'Characters | ' + CHARACTERCOUNT.toLocaleString() );
console.log( 'Characters (inc. spaces) | ' + CHARACTERCOUNTWITHSPACES.toLocaleString() );
console.log( 'Average word length | ' + ( CHARACTERCOUNT / WORDCOUNT ) );
console.log( '================================================' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment