Repository URL to install this package:
|
Version:
5.0.0-rc.5 ▾
|
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.updateMessages = exports.getDeletedKeys = exports.getRenamedKeys = exports.getChangedKeys = exports.getAddedKeys = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
/**
* Compare translation files
*
* @module @doodle/i18n/disty/onesky/diff
*/
/**
* Return an array containing an array of new keys based on the difference between source and OneSky base translation
*
* @param {object} sourceMessages hierarchical messages extracted from source files
* @param {object} baseMessages hierarchical messages from the base OneSky translation
* @return {string[]} an array of new keys
*/
const getAddedKeys = (sourceMessages, baseMessages) => {
const messageKeys = Object.keys(baseMessages);
const messageValues = Object.values(baseMessages);
const newEntries = Object.entries(sourceMessages).filter(([key]) => messageKeys.indexOf(key) < 0);
return newEntries.map(([key, value]) => messageValues.indexOf(value) >= 0 ? undefined : key).filter(k => k);
};
/**
* Return an array containing an array of keys for changed values based on the difference between source and OneSky base translation
*
* @param {object} sourceMessages hierarchical messages extracted from source files
* @param {object} baseMessages hierarchical messages from the base OneSky translation
* @return {string[]} an array of keys for changed valeus
*/
exports.getAddedKeys = getAddedKeys;
const getChangedKeys = (sourceMessages, baseMessages) => Object.entries(sourceMessages).filter(([key]) => baseMessages[key]).filter(([key, value]) => baseMessages[key] !== value).map(([k]) => k);
/**
* Return an array containing a [fromKey, toKey] tuple for renaming translation keys based on the difference between source and OneSky base translation
*
* @param {object} sourceMessages hierarchical messages extracted from source files
* @param {object} baseMessages hierarchical messages from the base OneSky translation
* @return {array[]} a [fromKey, toKey] tuple for renaming keys
*/
exports.getChangedKeys = getChangedKeys;
const getRenamedKeys = (sourceMessages, baseMessages) => {
const messageKeys = Object.keys(baseMessages);
const messageValues = Object.values(baseMessages);
const newEntries = Object.entries(sourceMessages).filter(([key]) => messageKeys.indexOf(key) < 0);
return newEntries.map(([key, value]) => messageValues.indexOf(value) >= 0 ? [messageKeys[messageValues.indexOf(value)], key] : []).filter(([k]) => k);
};
/**
* Return an array containing an array of deleted keys based on the difference between source and OneSky base translation
*
* @param {object} sourceMessages hierarchical messages extracted from source files
* @param {object} baseMessages hierarchical messages from the base OneSky translation
* @return {string[]} an array of deleted keys
*/
exports.getRenamedKeys = getRenamedKeys;
const getDeletedKeys = (sourceMessages, baseMessages) => {
const renamedKeys = getRenamedKeys(sourceMessages, baseMessages);
return Object.keys(baseMessages).filter(key => Object.keys(sourceMessages).indexOf(key) < 0).filter(key => renamedKeys.filter(([k]) => key === k).length === 0);
};
/**
* Return a new flat message object according to modification instructions.
* This can be used to modify message objects from other languages based on modification instructions from the base language
*
* @param {object} messages
* @param {object} modifications
* @param {array[]} modifications.added
* @param {array[]} modifications.changed
* @param {array[]} modifications.renamed
* @param {string[]} modifications.deleted
*/
exports.getDeletedKeys = getDeletedKeys;
const updateMessages = (messages, {
added = [],
changed = [],
renamed = [],
deleted = []
} = {}) => {
const result = _objectSpread({}, messages);
added.forEach(([key, value]) => {
result[key] = value;
});
changed.forEach(([key, value]) => {
result[key] = value;
});
renamed.forEach(([from, to]) => {
result[to] = messages[from];
delete result[from];
});
deleted.forEach(k => {
delete result[k];
});
return result;
};
exports.updateMessages = updateMessages;
//# sourceMappingURL=diff.js.map