Add .eq() usage to docs and fix docs typos (#1405)

* Use `eq` for example

* Use the base `eq` function rather than `===`, so that comparisons are made in a consistent manner in docs

* Add `eq` example

* Add an example of using the `eq` method to the base docs

* Adjust docs/examples

* Fix a number of typos in the static docs and examples
* Adjust various comments in docs examples such that they are written in a consistent case

* Fix metadata docs spacing

* Adjust the metadata documentation generation such that unnecessary spaces are trimmed
This commit is contained in:
James Lefrère
2019-09-19 11:43:47 +02:00
committed by Jaco Greeff
parent 229fd72526
commit 73d7a57d38
29 changed files with 152 additions and 130 deletions
+21 -4
View File
@@ -19,9 +19,26 @@ Additionally, the following getters and utilities are available -
- `.isEmpty` - `true` if the value is an all-empy value, i.e. `0` in for numbers, all-zero for Arrays (or anything `Uint8Array`), `false` is non-zero
- `.hash` - a `Hash` (once again with all the methods above) that is a `blake2-256` representation of the contained value
## Comparing types
To reiterate the above API, the `.eq` method is the preferred means of comparing base types, rather than the JavaScript equality operator (`===`).
For example:
```js
const { metadata } = await api.rpc.state.getMetadata();
const modules = metadata.asV3.modules;
// This will not work, because `name` is an instance of `Text`, not a string
// const system = modules.find(m => m.name === 'system');
// This will work, because `Text.eq()` can compare against a string
const system = modules.find(m => m.name.eq('system'));
```
## Working with numbers
All numbers wrap and extend an instance of [bn.js](https://github.com/indutny/bn.js/). This means that in addition to the interfaces defined above, they have some additional methods -
All numbers wrap and extend an instance of [bn.js](https://github.com/indutny/bn.js/). This means that in addition to the interfaces defined above, they have some additional methods -
- `.toNumber()` - a JS number (limited to 2^53 - 1). This does mean that for large values, e.g. `Balance` (a `u128` extension), this can cause overflows
- `.add(...)`, `.sub(...)`, ... - all the base methods available on the `BN` object
@@ -30,7 +47,7 @@ In cases where a `Compact` is returned, i.e. `Compact<Balance>`, the value is wr
## Working with structures
All structures, a wrapping of an object containing a number of member variables, is an implementation of a standard JS `Map` object, so all the functions available on a `Map` such as `.entries()` are available. Additionally it is decorated with actual getters for the fields.
All structures, a wrapping of an object containing a number of member variables, is an implementation of a standard JS `Map` object, so all the functions available on a `Map` such as `.entries()` are available. Additionally it is decorated with actual getters for the fields.
As an example, a `Header` will have getters for the `.parentHash`, `.number`, `.stateRoot`, `.extrinsicsRoot` and `.digest` fields. The same applies for all structures, as they are returned, each member will have an associated getter.
@@ -48,7 +65,7 @@ An `Option<Type>` attempts to mimic the Rust approach of having `None` and `Some
- `.isNone` - is `true` if no underlying values is warpped, effectively the same as `.isEmpty`
- `.isSome` - this is `true` is a value is wrapped, i.e. if a `Option<u32>` has an actual underlying `u32`
- `.unwrap()` - when `isSome`, this will return the wrapped value, i.e. for `Option<u32>`, this would return the `u32`. When the value is `isNone`, this call will throw an exception.
- `.unwrap()` - when `isSome`, this will return the wrapped value, i.e. for `Option<u32>`, this would return the `u32`. When the value is `isNone`, this call will throw an exception.
- `.unwrapOr(<default value>)` - this extends `unwrap()`, returning the wrapped value when `isSome` and in the case of `isNone` it will return the `<default value>` passed.
## Working with Tuples
@@ -56,7 +73,7 @@ An `Option<Type>` attempts to mimic the Rust approach of having `None` and `Some
A tuple is defined in the form of `(u32, AccountId)`. To access the individual values, you can access t via the index, i.e.
```js
// assuming a tuple defined as `(32, AccountId)`
// Assuming a tuple defined as `(32, AccountId)`
const [count, accountId] = tuple;
console.log(`${accountId} has ${count.toNumber()} values`);