-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add clone method * Update dist * Add docs about cloning * Move attribute setters/getters to the final so methods won't override them * Update dist * Bump minor
- Loading branch information
1 parent
aac5225
commit a58b63c
Showing
9 changed files
with
397 additions
and
44 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 |
---|---|---|
@@ -1 +1,3 @@ | ||
/dist | ||
/README.md | ||
/CHANGELOG.md |
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,27 +1,28 @@ | ||
# Table of contents | ||
|
||
* [Introduction](../README.md) | ||
* [Schema concept](schema-concept/README.md) | ||
* [Shorthand and complete type descriptor](schema-concept/shorthand-and-complete-type-descriptor.md) | ||
* [Circular reference](schema-concept/circular-references-and-dynamic-types.md) | ||
* [Nullable attributes](schema-concept/nullable-attributes.md) | ||
* [Coercion](coercion/README.md) | ||
* [Primitive type coercion](coercion/primitive-type-coercion.md) | ||
* [Arrays coercion](coercion/arrays-and-array-subclasses.md) | ||
* [Generic coercion](coercion/generic-coercion.md) | ||
* [Recursive coercion](coercion/recursive-coercion.md) | ||
* [Observations](coercion/observations.md) | ||
* [Validation](validation/README.md) | ||
* [String validations](validation/string-validations.md) | ||
* [Number validations](validation/number-validations.md) | ||
* [Boolean validations](validation/boolean-validations.md) | ||
* [Date validations](validation/date-validations.md) | ||
* [Array validations](validation/array-validations.md) | ||
* [Attribute reference](validation/attribute-reference.md) | ||
* [Nested validations](validation/nested-validations.md) | ||
* [Validate raw data](validation/validate-raw-data.md) | ||
* [Strict mode](strict-mode.md) | ||
* [Serialization](serialization.md) | ||
* [Contributing](../contributing.md) | ||
* [License](../license.md) | ||
* [GitHub](https://github.com/talyssonoc/structure) | ||
- [Introduction](../README.md) | ||
- [Schema concept](schema-concept/README.md) | ||
- [Shorthand and complete type descriptor](schema-concept/shorthand-and-complete-type-descriptor.md) | ||
- [Circular reference](schema-concept/circular-references-and-dynamic-types.md) | ||
- [Nullable attributes](schema-concept/nullable-attributes.md) | ||
- [Coercion](coercion/README.md) | ||
- [Primitive type coercion](coercion/primitive-type-coercion.md) | ||
- [Arrays coercion](coercion/arrays-and-array-subclasses.md) | ||
- [Generic coercion](coercion/generic-coercion.md) | ||
- [Recursive coercion](coercion/recursive-coercion.md) | ||
- [Observations](coercion/observations.md) | ||
- [Validation](validation/README.md) | ||
- [String validations](validation/string-validations.md) | ||
- [Number validations](validation/number-validations.md) | ||
- [Boolean validations](validation/boolean-validations.md) | ||
- [Date validations](validation/date-validations.md) | ||
- [Array validations](validation/array-validations.md) | ||
- [Attribute reference](validation/attribute-reference.md) | ||
- [Nested validations](validation/nested-validations.md) | ||
- [Validate raw data](validation/validate-raw-data.md) | ||
- [Strict mode](strict-mode.md) | ||
- [Cloning an instance](cloning.md) | ||
- [Serialization](serialization.md) | ||
- [Contributing](../contributing.md) | ||
- [License](../license.md) | ||
- [GitHub](https://github.com/talyssonoc/structure) |
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,78 @@ | ||
# Cloning an instance | ||
|
||
Structure adds a method `#clone` in order to be able to create a **shallow** copy of an instance. This methods accepts an optional overwrite object that permits you to overwrite some attributes of the copy. | ||
|
||
```js | ||
const { attributes } = require('structure'); | ||
|
||
const User = attributes({ | ||
name: String, | ||
})(class User {}); | ||
|
||
const user = new User({ | ||
name: 'Me', | ||
}); | ||
|
||
const cloneUserWithNoOverwrite = user.clone(); // User { name: 'Me } | ||
|
||
const cloneWithOverwrite = user.clone({ name: 'Myself' }); // User { name: 'Myself' } | ||
``` | ||
|
||
If the structure has a nested structure inside of it, the `#clone` method **will not** clone it but just point the new instance to the old value of the nested attribute. | ||
|
||
```js | ||
const { attributes } = require('structure'); | ||
|
||
const Book = attributes({ | ||
name: String, | ||
})(class Book {}); | ||
|
||
const User = attributes({ | ||
name: String, | ||
favoriteBook: Book, | ||
})(class User {}); | ||
|
||
const user = new User({ | ||
name: 'Me', | ||
favoriteBook: new Book({ name: 'The Silmarillion' }), | ||
}); | ||
|
||
const cloneUserWithNoOverwrite = user.clone(); | ||
cloneUserWithNoOverwrite.favoriteBook === user.favoriteBook; // true, it was not cloned | ||
|
||
const cloneWithOverwrite = user.clone({ | ||
favoriteBook: { name: 'The Lord of the Rings' }, | ||
}); | ||
cloneWithOverwrite.favoriteBook === user.favoriteBook; // false, it was **replaced** with the new value | ||
cloneWithOverwrite.favoriteBook; // Book { name: 'The Lord of the Rings' } | ||
``` | ||
|
||
## Strict mode | ||
|
||
When cloning an instance, you can clone it in [strict mode](strict-mode.md) as well, so if the resulting clone is invalid it throws an error. To do that, pass a second argument to the `#clone` method with the option `strict` as `true`. | ||
|
||
```js | ||
const { attributes } = require('structure'); | ||
|
||
const User = attributes({ | ||
name: { | ||
type: String, | ||
required: true, | ||
}, | ||
age: Number, | ||
})(class User {}); | ||
|
||
const user = new User({ | ||
name: 'Me', | ||
}); | ||
|
||
const clonedUser = user.clone( | ||
{ name: null }, | ||
{ strict: true } // strict mode option | ||
); | ||
|
||
// Error: Invalid Attributes | ||
// details: [ | ||
// { message: '"name" is required', path: 'name' } | ||
// ] | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
exports.buildCloneDescriptorFor = function buildCloneDescriptorFor( | ||
StructureClass | ||
) { | ||
return { | ||
configurable: true, | ||
value: function clone(overwrites = {}, options = {}) { | ||
const { strict } = options; | ||
|
||
const newAttributes = Object.assign({}, this.attributes, overwrites); | ||
|
||
let cloneInstance; | ||
|
||
if (strict) { | ||
cloneInstance = StructureClass.buildStrict(newAttributes); | ||
} else { | ||
cloneInstance = new StructureClass(newAttributes); | ||
} | ||
|
||
return cloneInstance; | ||
}, | ||
}; | ||
}; |
Oops, something went wrong.