Skip to content

Commit be390b6

Browse files
committed
sync
1 parent f38ccc9 commit be390b6

File tree

1 file changed

+87
-83
lines changed

1 file changed

+87
-83
lines changed

gruntfile.js

Lines changed: 87 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,97 @@ var _ = require('underscore');
22
var libumd = require('libumd');
33

44
var findDuplicates = function (sourceArray, prop) {
5-
var duplicates = [];
6-
var groupedByCount = _.countBy(sourceArray, function (item) { return item[prop]; });
7-
8-
for (var name in groupedByCount) {
9-
if (groupedByCount[name] > 1) {
10-
var whereClause = [];
11-
whereClause[prop] = name;
12-
_.where(sourceArray, whereClause).map(function (item) { duplicates.push(item); });
13-
}
14-
}
15-
16-
return _.uniq(_.pluck(duplicates, prop));
5+
var duplicates = [];
6+
var groupedByCount = _.countBy(sourceArray, function (item) {
7+
return item[prop];
8+
});
9+
10+
for (var name in groupedByCount) {
11+
if (groupedByCount[name] > 1) {
12+
var whereClause = [];
13+
whereClause[prop] = name;
14+
_.where(sourceArray, whereClause).map(function (item) {
15+
duplicates.push(item);
16+
});
17+
}
18+
}
19+
20+
return _.uniq(_.pluck(duplicates, prop));
1721
};
1822

1923
var getJSON = function (grunt) {
20-
var content = '';
21-
try {
22-
content = grunt.file.readJSON("data.json");
23-
} catch (e) {
24-
grunt.fail.fatal("data.json is not valid JSON. Error: " + e);
25-
}
26-
return content;
24+
var content = '';
25+
try {
26+
content = grunt.file.readJSON("data.json");
27+
} catch (e) {
28+
grunt.fail.fatal("data.json is not valid JSON. Error: " + e);
29+
}
30+
return content;
2731
};
2832

2933

30-
module.exports = function(grunt) {
31-
32-
function validate () {
33-
var content = getJSON(grunt);
34-
35-
// check country names and country shortcodes are unique
36-
var duplicateCountryNames = findDuplicates(content, 'countryName');
37-
if (duplicateCountryNames.length > 0) {
38-
grunt.fail.fatal('The country names are not unique - duplicates: ' + duplicateCountryNames);
39-
}
40-
var duplicateCountryShortCodes = findDuplicates(content, 'countryShortCode');
41-
if (duplicateCountryShortCodes.length > 0) {
42-
grunt.fail.fatal('The country short codes are not unique - duplicates: ' + duplicateCountryShortCodes);
43-
}
44-
45-
// now check region names and short codes are unique for each country
46-
content.forEach(function (countryData) {
47-
var duplicateRegionNames = findDuplicates(countryData.regions, 'name');
48-
if (duplicateRegionNames.length > 0) {
49-
grunt.fail.fatal('The region names for ' + countryData.countryName + ' are not unique - duplicates: ' + duplicateRegionNames);
50-
}
51-
});
52-
console.log("PASS!");
53-
}
54-
55-
56-
function findIncomplete () {
57-
var content = getJSON(grunt);
58-
59-
var incompleteCountries = [];
60-
content.forEach(function (countryData) {
61-
for (var i=0; i<countryData.regions.length; i++) {
62-
if (!_.has(countryData.regions[i], 'shortCode')) {
63-
incompleteCountries.push(countryData.countryName);
64-
break;
65-
}
66-
}
67-
});
68-
69-
if (incompleteCountries.length > 0) {
70-
console.log('\nThe following countries are missing region short codes: \n-', incompleteCountries.join('\n- '));
71-
console.log('\n(' + incompleteCountries.length + ' countries)');
72-
} else {
73-
console.log('All regions now have short codes. Nice!');
74-
}
75-
}
76-
77-
function umdify () {
78-
var content = getJSON(grunt);
79-
80-
var output = libumd("return " + JSON.stringify(content, null, 2) + ";", {
81-
globalAlias: "countryRegionData",
82-
indent: 2
83-
});
84-
85-
grunt.file.write("data.js", output);
86-
87-
console.log('Successfully made a UMD module!');
88-
}
89-
90-
grunt.registerTask("default", ['validate']);
91-
grunt.registerTask("validate", validate);
92-
grunt.registerTask("findIncomplete", findIncomplete);
93-
grunt.registerTask("umdify", umdify);
34+
module.exports = function (grunt) {
35+
36+
function validate () {
37+
var content = getJSON(grunt);
38+
39+
// check country names and country shortcodes are unique
40+
var duplicateCountryNames = findDuplicates(content, 'countryName');
41+
if (duplicateCountryNames.length > 0) {
42+
grunt.fail.fatal('The country names are not unique - duplicates: ' + duplicateCountryNames);
43+
}
44+
var duplicateCountryShortCodes = findDuplicates(content, 'countryShortCode');
45+
if (duplicateCountryShortCodes.length > 0) {
46+
grunt.fail.fatal('The country short codes are not unique - duplicates: ' + duplicateCountryShortCodes);
47+
}
48+
49+
// now check region names and short codes are unique for each country
50+
content.forEach(function (countryData) {
51+
var duplicateRegionNames = findDuplicates(countryData.regions, 'name');
52+
if (duplicateRegionNames.length > 0) {
53+
grunt.fail.fatal('The region names for ' + countryData.countryName + ' are not unique - duplicates: ' + duplicateRegionNames);
54+
}
55+
});
56+
console.log("PASS!");
57+
}
58+
59+
60+
function findIncomplete () {
61+
var content = getJSON(grunt);
62+
63+
var incompleteCountries = [];
64+
content.forEach(function (countryData) {
65+
for (var i = 0; i < countryData.regions.length; i++) {
66+
if (!_.has(countryData.regions[i], 'shortCode')) {
67+
incompleteCountries.push(countryData.countryName);
68+
break;
69+
}
70+
}
71+
});
72+
73+
if (incompleteCountries.length > 0) {
74+
console.log('\nThe following countries are missing region short codes: \n-', incompleteCountries.join('\n- '));
75+
console.log('\n(' + incompleteCountries.length + ' countries)');
76+
} else {
77+
console.log('All regions now have short codes. Nice!');
78+
}
79+
}
80+
81+
function umdify () {
82+
var content = getJSON(grunt);
83+
84+
var output = libumd("return " + JSON.stringify(content, null, 2) + ";", {
85+
globalAlias: "countryRegionData",
86+
indent: 2
87+
});
88+
89+
grunt.file.write("data.js", output);
90+
91+
console.log('Successfully made a UMD module!');
92+
}
93+
94+
grunt.registerTask("default", ['validate']);
95+
grunt.registerTask("validate", validate);
96+
grunt.registerTask("findIncomplete", findIncomplete);
97+
grunt.registerTask("umdify", umdify);
9498
};

0 commit comments

Comments
 (0)