Skip to content

Commit

Permalink
Handle JSON file with BOM.
Browse files Browse the repository at this point in the history
  • Loading branch information
sey committed Mar 22, 2016
1 parent 8dcdf94 commit b09ffb2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 36 deletions.
35 changes: 21 additions & 14 deletions .travis.yml
@@ -1,19 +1,26 @@
language: node_js
node_js:
- "4"
- "5"
- '4'
- '5'
before_install:
- npm install -g npm
- npm install -g npm
before_script:
- npm install -g jshint
- npm install -g mocha
- npm install -g istanbul
- npm install -g codeclimate-test-reporter
- npm install
script:
- npm run lint
- npm test
- npm run coverage
- npm install -g jshint
- npm install -g mocha
- npm install -g istanbul
- npm install -g codeclimate-test-reporter
- npm install
script:
- npm run lint
- npm test
- npm run coverage
after_success:
- npm run coveralls
- codeclimate-test-reporter < coverage/lcov.info
- npm run coveralls
- codeclimate-test-reporter < coverage/lcov.info
deploy:
provider: npm
on:
tags: true
email: "florian.sey@gmail.com"
api_key:
secure: JCVhjEr68WXcKAG7xBHAxT8uzpoHFeYVpwchfVLnDPqFQIcQp+Zlq6hKyVq4cBpoH7zjTtC4UWEObiSES50fnXOWVq3+dnvp/JjfQuGQfEt93YtYy8eatQ2SKVq5xxWn65t/l/ocEZ1/3RlFymVv5GgarApthtseMWvcYrgFdP/3YLEge27Qzhlcuas4XzSfT3ASZ/v3QFLes0Qnqzijbimmy20PJJ8EiQBuI6LoKzVg0NO8YPLRs3qrMEWjtugvqdDdZ2qD3UcQIcwZTCfRMwChmd2JB36DsqrsgFQB5XP+44rQ5p1tLaXBTEmpeNEXjdbr/Qd+u/wwckIxvyPHWBi3iFXcLWLYZuDOWWckAiTLSTNhPS1Hqh78ugzMS5UkmkF+rHg8FXnVsk0c467B2wrowq6vX3csI4k+PivTuEiPTA3pLu5qyW/BXKID3YSQ8Jpenkzp0wU10j8XhiUcUffOdws6rF0Y/RtkLSqSxD37hZAjHHfDjJ9hnVOoMTTrmnHjUWo5jl6H/t0vuBYlZ1gLlImdAcDnXG8+FkdLCMheXcXblkl2fhoB9DfAXZC1Bs8vNpFSV5jNwzo8wkEVQCb3jCRltvlllca5b5XjjaDGKhrH+jnWpaaik9o/M2KNQ1YsKk22LPP39k5D492IelKynPXA/vw9Z8PqOHQK7JE=
9 changes: 5 additions & 4 deletions lib/utils.js
Expand Up @@ -8,13 +8,14 @@

'use strict';

var fs = require('fs');
var _ = require('lodash');
var fs = require('fs');
var _ = require('lodash');
var stripbom = require('strip-bom');

function getAllowedKeyFilter(allowedKeys) {
return function allowedKeyFilter(emptyTranslation) {
for (var i = 0; i < allowedKeys.length; i++) {
if (emptyTranslation.key.indexOf(allowedKeys[i]) !== -1) {
if (emptyTranslation.key.indexOf(allowedKeys[i]) > -1) {
return false;
}
}
Expand Down Expand Up @@ -43,7 +44,7 @@ exports.JSONFilesToMap = function(sources) {

if (sources) {
sources.forEach(function(source, idx) {
var rawJSON = fs.readFileSync(source, 'utf-8');
var rawJSON = stripbom(fs.readFileSync(source));
var parsedJSON = JSON.parse(rawJSON);
results[idx] = {
fileName: source,
Expand Down
5 changes: 3 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "validate-json-locales",
"version": "0.1.0",
"version": "0.1.1",
"description": "Validate locales files in JSON format.",
"main": "index.js",
"scripts": {
Expand All @@ -25,7 +25,8 @@
},
"homepage": "https://github.com/seventy-three/validate-json-locales#readme",
"dependencies": {
"lodash": "^4.6.1"
"lodash": "^4.6.1",
"strip-bom": "^2.0.0"
},
"devDependencies": {
"coveralls": "^2.11.8",
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/fileWithBOM.json
@@ -0,0 +1,11 @@
{
"key1": {
"key2": "value2",
"key3": "value3",
"key7": "value7"
},
"key4": {
"key5": "value5",
"key6": "value6"
}
}
22 changes: 6 additions & 16 deletions test/test.js
Expand Up @@ -13,82 +13,72 @@ var utils = require('../lib/utils');

describe('#JSONFilesToMap', function() {
it('should return an empty array if no arguments is passed', function() {
// when
var results = utils.JSONFilesToMap();

// then
assert.deepEqual(results, []);
});

it('should return a correct object', function() {
// given
var sources = ['test/fixtures/fileA.json'];

// when
var results = utils.JSONFilesToMap(sources);

// then
assert.ok(results.length > 0);
assert.strictEqual(results[0].fileName, 'test/fixtures/fileA.json');
assert.strictEqual(results[0].parsedJSON['JSON.string.1'], 'string1');
});

it('should handle file with BOM', function () {
var sources = ['test/fixtures/fileWithBOM.json'];
var results = utils.JSONFilesToMap(sources);
assert.ok(results.length > 0);
});
});


describe('#compareMaps', function() {
it('should return an empty array if no arguments is passed', function() {
// when
var reports = utils.compareMaps();

// then
assert.deepEqual(reports, []);
});

it('should return an empty array if no differences are found', function() {
// given
var values = {"a": "a", "b": "b"};
var maps = [
{parsedJSON: values, fileName: 'fileA'},
{parsedJSON: values, fileName: 'fileB'}
];

// when
var reports = utils.compareMaps(maps);

// then
assert.deepEqual(reports, []);
});

it('should return the missing key if some differences are found', function() {
// given
var valueA = {"a": "a", "b": "b"};
var valueB = {"b": "b"};
var maps = [
{parsedJSON: valueA, fileName: 'fileA'},
{parsedJSON: valueB, fileName: 'fileB'}
];

// when
var reports = utils.compareMaps(maps);

// then
assert.equal(reports.length, 1);
});


it('should return the missing key if some differences are found inside of sub-objects', function() {
// given
var valueA = {"a": {"me":"a","mo":"a"}, "b": "b"};
var valueB = {"a": {"me":"a"}, "b": "b"};
var maps = [
{parsedJSON: valueA, fileName: 'fileA'},
{parsedJSON: valueB, fileName: 'fileB'}
];

// when
var reports = utils.compareMaps(maps);

// then
assert.equal(reports.length, 1);
assert.equal(reports[0].key, 'a.mo');
});
Expand Down

0 comments on commit b09ffb2

Please sign in to comment.