Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
superman2211 committed Feb 1, 2023
1 parent 4280eb6 commit 73995c3
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-hotels-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@xobj/core": patch
---

Update docs
131 changes: 112 additions & 19 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
Decoding and encoding **JavaScript** / **TypeScript** objects to compact binary format.

Available basic types:
- `null`
- `undefined`
- `null` [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/empty.test.ts)
- `undefined` [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/empty.test.ts)
- `Number` [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/numbers.test.ts)
- `BigInt` [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/bigint.test.ts)
- `Boolean`
- `String`
- `Symbol` [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/symbol.test.ts)
- `Object`
- `Array`
- `Function` [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/function.test.ts)
- `Map`
- `Set`
- `ArrayBuffer`
- `TypedArray`:
- `Boolean` [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/boolean.test.ts)
- `String` [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/string.test.ts)
- `Symbol` [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/symbol.test.ts) [via replacer](https://github.com/superman2211/xobj/blob/master/packages/core/test/replacer.test.ts)
- `Object` [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/object.test.ts)
- `Array` [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/array.test.ts)
- `Function` [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/function.test.ts) [via replacer](https://github.com/superman2211/xobj/blob/master/packages/core/test/replacer.test.ts)
- `Map` [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/map.test.ts)
- `Set` [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/set.test.ts)
- `ArrayBuffer` [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/array-buffer.test.ts)
- `TypedArray`: [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/typed-array.test.ts)
- `Uint8ClampedArray`
- `Uint8Array`
- `Uint16Array`
Expand All @@ -32,8 +32,8 @@ Available basic types:
- `Float32Array`
- `Float64Array`
- `DataView`
- `RegExp`
- `Date`
- `RegExp` [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/reg-exp.test.ts)
- `Date` [examples](https://github.com/superman2211/xobj/blob/master/packages/core/test/date.test.ts)

For all basic types used optimization for data minification.

Expand All @@ -49,7 +49,8 @@ yarn add @xobj/core
```

## Usage
Basic usage with default types:

### Basic usage with default types
```typescript
// import library methods
import { encode, decode } from '@xobj/core';
Expand Down Expand Up @@ -83,7 +84,7 @@ console.log(target.name);// John Doe
console.log(target!.children![0].age);// 12
```

Custom types usage:
### Custom types usage
```typescript
import { encode, decode, EncodeContext, DecodeContext, ValueType } from '@xobj/core';

Expand Down Expand Up @@ -150,13 +151,14 @@ console.log(target.points[0].y) // 2
```
See more about [BufferWriter and BufferReader](https://github.com/superman2211/xobj/tree/master/packages/buffer)

Encode options
### Encode options
```typescript
encode(value, {
bufferSize, // buffer starter size
bufferSize, // buffer starter size, by default is 1024 bytes
customDetect, // function for custom type detection
customEncode, // function for custom type encoding
floatQuality, // encoding float quality : 'double' | 'single' | number (default is 'double')
replacer, // replacer method or table: (value: any) => any | Map<any,any> | map entries
});
```
The `floatQuality` parameter allows you to select the encoding type for floating point numbers.
Expand All @@ -183,17 +185,108 @@ const value = decode(buffer);
// read intVar from 3 bytes => -345678 / 100 => -3456.78
```

Decode options
### Decode options
```typescript
decode(value, {
customDecode, // function for custom type decoding
replacer, // replacer method or table: (value: any) => any | Map<any,any> | map entries
});
```

You can see more examples in [tests](https://github.com/superman2211/xobj/tree/master/packages/core/test).

Check out integration [samples](https://github.com/superman2211/xobj/tree/master/samples).

### Replacers
You can use 3 replacer types

Via function:
```typescript
const id = Symbol('id');

const source = {
x: 1,
update(value: number) {
this.x += value;
},
id,
};

const buffer = encode(source, {
replacer: (value) => {
if (value === id) return 'id-0';
if (value === source.update) return 345678;
return value;
},
});

const target = decode(buffer, {
replacer: (value) => {
if (value === 'id-0') return id;
if (value === 345678) return source.update;
return value;
},
});
```
Via `Map` table:
```typescript
const id = Symbol('id');

const source = {
x: 1,
update(value: number) {
this.x += value;
},
id,
};

const buffer = encode(source, {
replacer: new Map<any, any>([
[id, 'id-0'],
[source.update, 345678],
]),
});

const target = decode(buffer, {
replacer: new Map<any, any>([
['id-0', id],
[345678, source.update],
]),
});
```
Via map entries table:
```typescript
const id = Symbol('id');

const source = {
x: 1,
update(value: number) {
this.x += value;
},
id,
};

const buffer = encode(source, {
replacer: [
[id, 'id-0'],
[source.update, 345678],
],
});

const target = decode(buffer, {
replacer: [
['id-0', id],
[345678, source.update],
],
});
```

## Samples
- Rollup bundle sample [project](https://github.com/superman2211/xobj/blob/master/samples/rollup-sample) / [build](https://superman2211.github.io/xobj/samples/rollup-sample/dist/iife/index.html)
- Rollup with external module [project](https://github.com/superman2211/xobj/tree/master/samples/rollup-external-sample) / [build](https://superman2211.github.io/xobj/samples/rollup-external-sample/dist/iife/index.html)
- Simple JS lib sample [project](https://github.com/superman2211/xobj/tree/master/samples/browser-sample) / [build](https://superman2211.github.io/xobj/samples/browser-sample/dist/index.html)
- NodeJS sample [project](https://github.com/superman2211/xobj/tree/master/samples/nodejs-sample)

## File format (xobj)
Coming soon

Expand Down

0 comments on commit 73995c3

Please sign in to comment.