-
-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
1,965 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"presets": ["env"], | ||
"sourceMaps": true, | ||
"retainLines": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# EditorConfig helps developers define and maintain consistent | ||
# coding styles between different editors and IDEs | ||
# editorconfig.org | ||
|
||
root = true | ||
|
||
|
||
[*] | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 2 | ||
|
||
[*.{diff,md}] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Mocha Tests", | ||
"cwd": "${workspaceRoot}", | ||
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha", | ||
"args": [ | ||
"-u", "tdd", | ||
"--timeout", "999999", | ||
"--colors", | ||
"--require", "babel-register", | ||
"--require", "babel-polyfill", | ||
"--plugins", "transform-object-rest-spread", | ||
"${workspaceFolder}/test/**/*.test.js" | ||
], | ||
"runtimeArgs": [ | ||
"--nolazy" | ||
], | ||
"sourceMaps": true, | ||
"internalConsoleOptions": "openOnSessionStart" | ||
}, | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Launch Program", | ||
"program": "${workspaceFolder}/src/helpers.js" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,107 @@ | ||
import _ from 'lodash' | ||
|
||
// Takes a `path` of the form 'foo.bar' and | ||
// turn it into a hash {foo: {bar: ""}}. | ||
// The generated hash can be attached to an | ||
// optional `hash`. | ||
function hashFromString(path, separator, value, hash) { | ||
separator = separator || '.'; | ||
|
||
if ( path.indexOf( separator, path.length - separator.length ) >= 0 ) { | ||
path = path.slice( 0, -separator.length ); | ||
function dotPathToHash(path, separator = '.', value = '', target = {}) { | ||
if (path.endsWith(separator)) { | ||
path = path.slice( 0, -separator.length ) | ||
} | ||
|
||
var parts = path.split( separator ); | ||
var tmp_obj = hash || {}; | ||
var obj = tmp_obj; | ||
|
||
for( var x = 0; x < parts.length; x++ ) { | ||
if ( x == parts.length - 1 ) { | ||
tmp_obj[parts[x]] = value || ''; | ||
} | ||
else if ( ! tmp_obj[parts[x]] ) { | ||
tmp_obj[parts[x]] = {}; | ||
} | ||
tmp_obj = tmp_obj[parts[x]]; | ||
} | ||
return obj; | ||
let result = {} | ||
const segments = path.split(separator) | ||
|
||
segments.reduce((hash, segment, index) => { | ||
if (index === segments.length - 1) { | ||
hash[segment] = value | ||
} | ||
else { | ||
hash[segment] = {} | ||
} | ||
return hash[segment] | ||
}, result) | ||
|
||
return _.merge(target, result) | ||
} | ||
|
||
|
||
// Takes a `source` hash and make sure its value | ||
// are pasted in the `target` hash, if the target | ||
// hash has the corresponding key (or if keepRemoved is true). | ||
// If not, the value is added to an `old` hash. | ||
function mergeHash(source, target, old, keepRemoved) { | ||
target = target || {}; | ||
old = old || {}; | ||
|
||
Object.keys(source).forEach(function (key) { | ||
if ( target[key] !== undefined ) { | ||
if (typeof source[key] === 'object' && source[key].constructor !== Array) { | ||
var nested = mergeHash( source[key], target[key], old[key], keepRemoved ); | ||
target[key] = nested.new; | ||
old[key] = nested.old; | ||
} | ||
else { | ||
target[key] = source[key]; | ||
} | ||
} | ||
else { | ||
// support for plural in keys | ||
pluralMatch = /_plural(_\d+)?$/.test( key ); | ||
singularKey = key.replace( /_plural(_\d+)?$/, '' ); | ||
|
||
// support for context in keys | ||
contextMatch = /_([^_]+)?$/.test( singularKey ); | ||
rawKey = singularKey.replace( /_([^_]+)?$/, '' ); | ||
|
||
if ( | ||
( contextMatch && target[rawKey] !== undefined ) || | ||
( pluralMatch && target[singularKey] !== undefined ) | ||
) { | ||
target[key] = source[key]; | ||
} | ||
else { | ||
if (keepRemoved) { | ||
target[key] = source[key]; | ||
} | ||
old[key] = source[key]; | ||
} | ||
} | ||
}); | ||
|
||
return { | ||
'new': target, | ||
'old': old | ||
}; | ||
function mergeHashes(source, target = {}, old, keepRemoved = false) { | ||
old = old || {} | ||
Object.keys(source).forEach((key) => { | ||
const hasNestedEntries = ( | ||
typeof target[key] === 'object' && | ||
!Array.isArray(target[key]) | ||
) | ||
|
||
if (hasNestedEntries) { | ||
const nested = mergeHashes(source[key], target[key], old[key], keepRemoved) | ||
target[key] = nested.new | ||
old[key] = nested.old | ||
} | ||
else if ( target[key] !== undefined ) { | ||
if (typeof source[key] === 'string' || Array.isArray(source[key])) { | ||
target[key] = source[key] | ||
} | ||
else { | ||
old[key] = source[key] | ||
} | ||
} | ||
else { | ||
// support for plural in keys | ||
const pluralMatch = /_plural(_\d+)?$/.test( key ) | ||
const singularKey = key.replace(/_plural(_\d+)?$/, '') | ||
|
||
// support for context in keys | ||
const contextMatch = /_([^_]+)?$/.test(singularKey) | ||
const rawKey = singularKey.replace(/_([^_]+)?$/, '') | ||
|
||
if ( | ||
(contextMatch && target[rawKey] !== undefined) || | ||
(pluralMatch && target[singularKey] !== undefined) | ||
) { | ||
target[key] = source[key] | ||
} | ||
else if (keepRemoved) { | ||
target[key] = source[key] | ||
old[key] = source[key] | ||
} | ||
else { | ||
old[key] = source[key] | ||
} | ||
} | ||
}) | ||
|
||
return {old, new: target} | ||
} | ||
|
||
|
||
// Takes a `target` hash and replace its empty | ||
// values with the `source` hash ones if they | ||
// exist | ||
function replaceEmpty(source, target) { | ||
target = target || {}; | ||
|
||
Object.keys(source).forEach(function (key) { | ||
if ( target[key] !== undefined ) { | ||
if (typeof source[key] === 'object') { | ||
var nested = replaceEmpty( source[key], target[key] ); | ||
target[key] = nested; | ||
} | ||
else if ( target[key] === '' ) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
}); | ||
|
||
return target; | ||
function populateHash(source, target = {}) { | ||
Object.keys(source).forEach((key) => { | ||
if ( target[key] !== undefined ) { | ||
if (typeof source[key] === 'object') { | ||
target[key] = populateHash(source[key], target[key]) | ||
} | ||
else if (target[key] === '') { | ||
target[key] = source[key] | ||
} | ||
} | ||
}) | ||
|
||
return target | ||
} | ||
|
||
|
||
|
||
module.exports = { | ||
hashFromString: hashFromString, | ||
mergeHash: mergeHash, | ||
replaceEmpty: replaceEmpty | ||
}; | ||
export { | ||
dotPathToHash, | ||
mergeHashes, | ||
populateHash | ||
} |
Oops, something went wrong.