Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
refactor(config): use lodash.get .set
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jul 7, 2016
1 parent b14e22f commit d5e3eb3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 40 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@
"fs-blob-store": "^5.2.1",
"glob": "^7.0.3",
"hapi": "^13.4.1",
"ipfs-bitswap": "^0.6.0",
"ipfs-api": "ipfs/js-ipfs-api#ebb7996",
"ipfs-bitswap": "^0.6.0",
"ipfs-block": "^0.3.0",
"ipfs-block-service": "^0.4.0",
"ipfs-merkle-dag": "^0.6.0",
Expand All @@ -84,8 +84,8 @@
"lodash.get": "^4.3.0",
"lodash.set": "^4.2.0",
"mafmt": "^2.1.1",
"multihashes": "^0.2.2",
"multiaddr": "^2.0.2",
"multihashes": "^0.2.2",
"path-exists": "^3.0.0",
"peer-book": "^0.3.0",
"peer-id": "^0.7.0",
Expand Down
51 changes: 13 additions & 38 deletions src/core/ipfs/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'

const promisify = require('promisify-es6')
const get = require('lodash.get')
const set = require('lodash.set')

module.exports = function config (self) {
return {
Expand All @@ -22,22 +24,14 @@ module.exports = function config (self) {
if (err) {
return callback(err)
}
const keys = key.split('.')
let finished = false
keys.forEach((key) => {
if (finished) {
return
}
if (config[key]) {
config = config[key]
} else {
finished = true
callback(new Error(('Key does not exist in config')))
}
})
if (!finished) {
callback(null, config)

const val = get(config, key)

if (val === undefined) {
return callback(new Error('Key does not exist in config'))
}

callback(null, val)
})
}),
set: promisify((key, value, callback) => {
Expand All @@ -50,33 +44,14 @@ module.exports = function config (self) {
}

self._repo.config.get((err, config) => {
const configBak = config
if (err) {
return callback(err)
}
const keys = key.split('.')
let finished = false
keys.forEach((key, index) => {
if (finished) {
return
}
if (config[key]) {
if (index === keys.length - 1) {
finished = true
config[key] = value
}
config = config[key]
} else {
if (index === keys.length - 1) {
finished = true
config[key] = value
} else {
config = config[key] = {}
}
}
})

self.config.replace(configBak, callback)
self.config.replace(
set(config, key, value),
callback
)
})
}),
replace: promisify((config, callback) => {
Expand Down

0 comments on commit d5e3eb3

Please sign in to comment.