-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(repl): extract utility from repl main file
- Loading branch information
1 parent
2c4aa9f
commit 1cc12ac
Showing
3 changed files
with
52 additions
and
11 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,14 @@ | ||
/** | ||
* Similar to `Object.assign` but copying properties descriptors from `source` | ||
* as well. | ||
*/ | ||
export function assignToObject<T, U>(target: T, source: U): T & U { | ||
Object.defineProperties( | ||
target, | ||
Object.keys(source).reduce((descriptors, key) => { | ||
descriptors[key] = Object.getOwnPropertyDescriptor(source, key); | ||
return descriptors; | ||
}, Object.create(null)), | ||
); | ||
return target as T & U; | ||
} |
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,36 @@ | ||
import { expect } from 'chai'; | ||
import { assignToObject } from '@nestjs/core/repl/assign-to-object.util'; | ||
|
||
describe('assignToObject', () => { | ||
it('should copy all enumerable properties and their descriptors', () => { | ||
const sourceObj = {}; | ||
Object.defineProperty(sourceObj, 'foo', { | ||
value: 123, | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
}); | ||
Object.defineProperty(sourceObj, 'bar', { | ||
value: 456, | ||
configurable: true, | ||
enumerable: true, | ||
writable: false, | ||
}); | ||
const targetObj = {}; | ||
|
||
assignToObject(targetObj, sourceObj); | ||
|
||
expect(Object.getOwnPropertyDescriptor(targetObj, 'foo')).to.be.eql({ | ||
value: 123, | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
}); | ||
expect(Object.getOwnPropertyDescriptor(targetObj, 'bar')).to.be.eql({ | ||
value: 456, | ||
configurable: true, | ||
enumerable: true, | ||
writable: false, | ||
}); | ||
}); | ||
}); |