Skip to content

Instantly share code, notes, and snippets.

@expressiveco
Last active May 30, 2018 13:03
Show Gist options
  • Save expressiveco/56cb9d0531821d237d78b99efa40a1cb to your computer and use it in GitHub Desktop.
Save expressiveco/56cb9d0531821d237d78b99efa40a1cb to your computer and use it in GitHub Desktop.
Merging json files
  • For merging json file there are lot of packages like "merge-jsons-webpack-plugin" but they restrict the file extension to json.
  • But, datatables languages files have .lang extension(symbolic links might be considered but it might be cumbersome or useless effort)
  • So, i decided to write it straight forward and come up with this code.
  • I take advantage of CopyWebpackPlugin and did not write an extra webpack plugin.
/*
Extra translations for Turkish i.e. select.
*/
{
"select": {
"rows": {
"_": "%d kayıt seçildi",
"0": "",
"1": "1 kayıt seçildi"
}
}
}
const webpackLib = require("./webpack.lib.js");
plugins: [
new CopyWebpackPlugin([
{
from: 'node_modules/datatables.net-plugins/i18n/Turkish.lang',
transform: (content, path) => {
// Load extra language translations.
const myDataTablesTurkishLang = webpackLib.readJSON("./Scripts/Turkish-Extra.lang");
var dataTablesTurkishLang = webpackLib.parseJSON(content.toString('utf8'));
var mergedObj = Object.assign(dataTablesTurkishLang, myDataTablesTurkishLang);
return JSON.stringify(mergedObj, null, 2);
},
to: 'DataTables-Lang/Turkish.json',
}]),
]
const stripJSONComments = require("strip-json-comments");
const fs = require("fs");
exports.readJSON = (jsonFile) => {
var data = fs.readFileSync(jsonFile, 'utf8').replace(/^\uFEFF/, '');
return JSON.parse(stripJSONComments(data));
}
exports.parseJSON = (jsonString) => {
return JSON.parse(stripJSONComments(jsonString.replace(/^\uFEFF/, '')));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment