MCPcopy
hub / github.com/mashpie/i18n-node / localeMutator

Function localeMutator

i18n.js:1154–1230  ·  view source on GitHub ↗
(locale, singular, allowBranching)

Source from the content-addressed store, hash-verified

1152 * invoked, the targeted translation term will be set to the given value inside the locale table.
1153 */
1154 const localeMutator = function (locale, singular, allowBranching) {
1155 // Bail out on non-existent locales to defend against internal errors.
1156 if (!locales[locale]) return Function.prototype
1157
1158 // Handle object lookup notation
1159 const indexOfDot = objectNotation && singular.lastIndexOf(objectNotation)
1160 if (objectNotation && indexOfDot > 0 && indexOfDot < singular.length - 1) {
1161 // If branching wasn't specifically allowed, disable it.
1162 if (typeof allowBranching === 'undefined') allowBranching = false
1163 // This will become the function we want to return.
1164 let accessor = null
1165 // An accessor that takes one argument and returns null.
1166 const nullAccessor = () => null
1167 // Fix object path.
1168 let fixObject = () => ({})
1169 // Are we going to need to re-traverse the tree when the mutator is invoked?
1170 let reTraverse = false
1171 // Split the provided term and run the callback for each subterm.
1172 singular.split(objectNotation).reduce((object, index) => {
1173 // Make the mutator do nothing.
1174 accessor = nullAccessor
1175 // If our current target object (in the locale tree) doesn't exist or
1176 // it doesn't have the next subterm as a member...
1177 if (
1178 object === null ||
1179 !Object.prototype.hasOwnProperty.call(object, index)
1180 ) {
1181 // ...check if we're allowed to create new branches.
1182 if (allowBranching) {
1183 // Fix `object` if `object` is not Object.
1184 if (object === null || typeof object !== 'object') {
1185 object = fixObject()
1186 }
1187 // If we are allowed to, create a new object along the path.
1188 object[index] = {}
1189 } else {
1190 // If we aren't allowed, remember that we need to re-traverse later on and...
1191 reTraverse = true
1192 // ...return null to make the next iteration bail our early on.
1193 return null
1194 }
1195 }
1196 // Generate a mutator for the current level.
1197 accessor = (value) => {
1198 object[index] = value
1199 return value
1200 }
1201 // Generate a fixer for the current level.
1202 fixObject = () => {
1203 object[index] = {}
1204 return object[index]
1205 }
1206
1207 // Return a reference to the next deeper level in the locale tree.
1208 return object[index]
1209 }, locales[locale])
1210
1211 // Return the final mutator.

Callers 1

translateFunction · 0.85

Calls 1

fixObjectFunction · 0.85

Tested by

no test coverage detected