Skip to content

Instantly share code, notes, and snippets.

@pquentin
Created August 28, 2014 13:39
Show Gist options
  • Save pquentin/4c1bc87cc0375c66de4d to your computer and use it in GitHub Desktop.
Save pquentin/4c1bc87cc0375c66de4d to your computer and use it in GitHub Desktop.
First draft of unusedstrings.py in JS
"use strict";
let fs = require('fs'),
_ = require('underscore');
function match_stuff(regexp, data, cb) {
let match;
while ((match = regexp.exec(data)) !== null) {
cb(match[1]);
}
}
function getCodeStrings(filePath) {
let strings = []
var files = fs.readdirSync(__dirname + filePath);
files.forEach(function(file) {
var data = fs.readFileSync(__dirname + filePath + file);
var add_to_string = function(match) {
if (strings.indexOf(match) == -1) {
strings.push(match);
}
};
match_stuff(/__\("([^"]+)/g, data, add_to_string);
match_stuff(/__\('([^']+)/g, data, add_to_string);
});
return strings;
}
function getVideoStrings(filePath) {
var strings = [];
var videosJSON = require(__dirname + filePath);
for (let section_id in videosJSON) {
let section = videosJSON[section_id];
strings.push(section.title);
for (let video_id in section.videos) {
let video = section.videos[video_id];
strings.push(video.title);
if (video.description) {
strings.push(video.description);
}
if ('assertions' in video) {
for (let assertion_id in video.assertions) {
let assertion = video.assertions[assertion_id];
strings.push(assertion.title);
Array.prototype.push.apply(strings, assertion.hints);
}
}
}
}
return strings;
}
function getLocaleKeys(filePath) {
var strings = []
var localeFile = fs.readFileSync(__dirname + filePath);
let localeJSON = JSON.parse(localeFile);
for(let key in localeJSON) {
strings.push(key);
}
return strings;
}
let codeStrings = getCodeStrings('/../views/');
let videoStrings = getVideoStrings('/../data/videos.json');
let liveStrings = _.union(codeStrings, videoStrings);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment