forked from dcmjs-org/dcmjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dcmjs): Add a set of accessors to the sequence list so the API is…
… more consistent (dcmjs-org#224) * fix(dcmjs): Add a set of accessors to the sequence list so the API is more consistent. * fix(dcmjs): Added documentation on the addAccessors as requested
- Loading branch information
1 parent
7a7df33
commit 70b2433
Showing
5 changed files
with
77 additions
and
12 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
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,26 @@ | ||
/** | ||
* Adds accessors (set/get) to dest for every property in src. | ||
* @example | ||
* src = [{a:5,b:'string', c:null}] | ||
* addAccessors(src) | ||
* src.c = 'outerChange' | ||
* src[0].b='innerChange' | ||
* | ||
* assert src.a===5 | ||
* assert src[0].c === 'outerChange' | ||
* assert src.b === 'innerChange' | ||
*/ | ||
const addAccessors = (dest, src) => { | ||
Object.keys(src).forEach(key => { | ||
Object.defineProperty(dest, key, { | ||
get: () => { | ||
return src[key]; | ||
}, | ||
set: v => { | ||
src[key] = v; | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
export default addAccessors; |
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,11 +1,13 @@ | ||
import TID1500 from "./TID1500/index.js"; | ||
import TID300 from "./TID300/index.js"; | ||
import message from "./Message.js"; | ||
import addAccessors from "./addAccessors.js"; | ||
|
||
const utilities = { | ||
TID1500, | ||
TID300, | ||
message | ||
message, | ||
addAccessors | ||
}; | ||
|
||
export default utilities; |
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,5 +1,30 @@ | ||
const expect = require("chai").expect; | ||
const dcmjs = require("../build/dcmjs"); | ||
const { utilities } = dcmjs; | ||
const { addAccessors } = utilities; | ||
|
||
exports.test = () => { | ||
console.log('no tests yet'); | ||
const tests = { | ||
testAddAccessor: () => { | ||
const baseValue = { a: 1, b: 2 }; | ||
const arrValue = [baseValue]; | ||
addAccessors(arrValue, baseValue); | ||
expect(arrValue.a).to.equal(1); | ||
baseValue.a = 3; | ||
expect(arrValue.a).to.equal(3); | ||
arrValue.b = 4; | ||
expect(baseValue.b).to.equal(4); | ||
const forArr = []; | ||
arrValue.forEach(item => forArr.push(item)); | ||
expect(forArr.length).to.equal(1); | ||
}, | ||
}; | ||
|
||
Object.keys(tests).forEach(testKey => { | ||
console.log('Running', testKey); | ||
const test = tests[testKey]; | ||
test(); | ||
console.log('Done running', testKey); | ||
}); | ||
} | ||
|