Skip to content

Commit

Permalink
Sorts data.json so nodes, compoenents, interfaces, events, properties…
Browse files Browse the repository at this point in the history
…, methods, implementors etc. are in alphabetical order
  • Loading branch information
markwpearce committed Apr 11, 2022
1 parent ccd49e2 commit de0e9ce
Show file tree
Hide file tree
Showing 2 changed files with 12,596 additions and 12,548 deletions.
50 changes: 49 additions & 1 deletion scripts/scrape-roku-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ class Runner {
this.repairReferences();
this.linkMissingImplementers();

//sort arrays internal to the data
this.sortInternalData();

//store the output
fsExtra.outputFileSync(outPath, JSON.stringify(this.result, null, 4));
fsExtra.outputFileSync(outPath, JSON.stringify(this.result, objectKeySorter, 4));
}

/**
Expand Down Expand Up @@ -136,6 +139,36 @@ class Runner {
}
}

/**
* Sorts internal arrays of the data in results, eg. implementers, properties, methods, etc.
*/
public sortInternalData() {
const nameComparer = (a: { name: string }, b: { name: string }) => (a.name.localeCompare(b.name));
for (let component of Object.values(this.result.components)) {
component.constructors.sort((a, b) => b.params.length - a.params.length);
component.events.sort(nameComparer);
component.interfaces.sort(nameComparer);
}

for (let evt of Object.values(this.result.events)) {
evt.implementers.sort(nameComparer);
evt.properties.sort(nameComparer);
evt.methods.sort(nameComparer);
}

for (let iface of Object.values(this.result.interfaces)) {
iface.implementers.sort(nameComparer);
iface.properties.sort(nameComparer);
iface.methods.sort(nameComparer);
}

for (let node of Object.values(this.result.nodes)) {
node.events.sort(nameComparer);
node.fields.sort(nameComparer);
node.interfaces.sort(nameComparer);
}
}

public buildRoSGNodeList() {
// const asdf = this.httpGet('https://devtools.web.roku.com/schema/RokuSceneGraph.xsd');
}
Expand Down Expand Up @@ -670,6 +703,21 @@ function repairMarkdownLinks(text: string) {
return text;
}

/**
* Replacer function for JSON.Stringify to sort keys in objects
* Note - this ignores the top level properties
* from: https://gist.github.com/davidfurlong/463a83a33b70a3b6618e97ec9679e490
*/
function objectKeySorter(key, value) {
return (value instanceof Object && !(value instanceof Array)) && !!key
? Object.keys(value)
.sort()
.reduce((sorted, key) => {
sorted[key] = value[key];
return sorted;
}, {})
: value;
}

/**
* A class to help manage the parsed markdown tokens
Expand Down

0 comments on commit de0e9ce

Please sign in to comment.