Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 40 additions & 82 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,22 @@ POLO's partially encoding and field order based indexing (and string based index
## Examples
### Polorizer
```javascript
import { Polorizer, schema } from 'js-polo';

const fruit = {
name: 'orange',
cost: 300,
alias: ['tangerine', 'mandarin']
};

const schema = {
kind: 'struct',
fields: {
name: {
kind: 'string'
},
cost: {
kind: 'integer'
},
alias: {
kind: 'array',
fields: {
values: {
kind: 'string',
}
}
}
}
}
const structSchema = schema.struct({
name: schema.string,
cost: schema.integer,
alias: schema.arrayOf(schema.string)
});

const polorizer = new Polorizer();
polorizer.polorize(fruit, schema);
polorizer.polorize(fruit, structSchema);
console.log(polorizer.bytes())

// Output:
Expand All @@ -92,29 +80,17 @@ console.log(polorizer.bytes())

### Depolorizer
```javascript
import { Depolorizer, schema } from 'js-polo';

const wire = new Uint8Array([14, 79, 6, 99, 142, 1, 111, 114, 97, 110, 103, 101, 1, 44, 63, 6, 150, 1, 116, 97, 110, 103, 101, 114, 105, 110, 101, 109, 97, 110, 100, 97, 114, 105, 110])
const schema = {
kind: 'struct',
fields: {
name: {
kind: 'string'
},
cost: {
kind: 'integer'
},
alias: {
kind: 'array',
fields: {
values: {
kind: 'string',
}
}
}
}
}
const structSchema = schema.struct({
name: schema.string,
cost: schema.integer,
alias: schema.arrayOf(schema.string)
})

const depolorizer = new Depolorizer(wire)
console.log(depolorizer.depolorize(schema))
console.log(depolorizer.depolorize(structSchema))

// Output:
/*
Expand All @@ -128,29 +104,23 @@ console.log(depolorizer.depolorize(schema))

### Document Encoding
```javascript
import { Document, documentEncode, schema } from 'js-polo';

// Create a Fruit object
const fruit = {
name: 'orange',
cost: 300,
alias: ['tangerine', 'mandarin']
};

const schema = {
kind: 'struct',
fields: {
name: { kind: 'string' },
cost: { kind: 'integer' },
alias: {
kind: 'array',
fields: {
values: { kind: 'string' }
}
}
}
};
const structSchema = schema.struct({
name: schema.string,
cost: schema.integer,
alias: schema.arrayOf(schema.string)
});

// Encode the object into a Document
const document = documentEncode(fruit, schema);
const document = documentEncode(fruit, structSchema);

console.log(document.getData());
console.log(document.bytes());
Expand Down Expand Up @@ -183,28 +153,22 @@ console.log(document.bytes());

#### Decode Document
```javascript
import { Document, documentDecode, schema } from 'js-polo';

const wire = new Uint8Array([
13, 175, 1, 6, 85, 182, 3, 245, 3, 166, 4, 229, 4, 97, 108, 105,
97, 115, 14, 63, 6, 150, 1, 116, 97, 110, 103, 101, 114, 105, 110,
101, 109, 97, 110, 100, 97, 114, 105, 110, 99, 111, 115, 116,
3, 1, 44, 110, 97, 109, 101, 6, 111, 114, 97, 110, 103, 101
]);

const schema = {
kind: 'struct',
fields: {
name: { kind: 'string' },
cost: { kind: 'integer' },
alias: {
kind: 'array',
fields: {
values: { kind: 'string' }
}
}
}
};
const structSchema = schema.struct({
name: schema.string,
cost: schema.integer,
alias: schema.arrayOf(schema.string)
});

const document = new Document(wire, schema);
const document = new Document(wire, structSchema);

console.log(document.getData());

Expand All @@ -228,27 +192,21 @@ console.log(document.getData());

#### Decode Struct
```javascript
import { Depolorizer, schema } from 'js-polo';

const wire = new Uint8Array([
13, 175, 1, 6, 85, 182, 3, 245, 3, 166, 4, 229, 4, 97, 108, 105, 97, 115, 14, 63, 6, 150, 1, 116, 97, 110, 103, 101, 114, 105, 110, 101, 109, 97, 110, 100, 97, 114, 105, 110, 99, 111, 115, 116, 3, 1, 44, 110, 97, 109, 101, 6, 111, 114, 97, 110, 103, 101
]);

const schema = {
kind: 'struct',
fields: {
name: { kind: 'string' },
cost: { kind: 'integer' },
alias: {
kind: 'array',
fields: {
values: { kind: 'string' }
}
}
}
};
const structSchema = schema.struct({
name: schema.string,
cost: schema.integer,
alias: schema.arrayOf(schema.string)
});

const depolorizer = new Depolorizer(wire);

console.log(depolorizer.depolorize(schema));
console.log(depolorizer.depolorize(structSchema));

// Output:
/*
Expand Down
56 changes: 14 additions & 42 deletions __benchmarks__/polorizer.bench.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import Benchmark from 'benchmark';
import { Depolorizer } from '../src/depolorizer';
import { Polorizer } from '../src/polorizer';
import { documentEncode } from '../src/document';
import { Depolorizer, documentEncode, Polorizer, schema, type Schema } from '../src';

const mixedObject = {
a: 'Sins & Virtues',
Expand All @@ -14,71 +12,45 @@ const mixedObject = {
e: 45.23
};

const schema = {
kind: 'struct',
fields: {
a: {
kind: 'string'
},
b: {
kind: 'integer'
},
c: {
kind: 'array',
fields: {
values: {
kind: 'string'
}
}
},
d: {
kind: 'map',
fields: {
keys: {
kind: 'string'
},
values: {
kind: 'string'
}
}
},
e: {
kind: 'float'
}
}
};
const structSchema: Schema = schema.struct({
a: schema.string,
b: schema.integer,
c: schema.arrayOf(schema.string),
d: schema.map({ keys: schema.string, values: schema.string }),
e: schema.float
});

(():void => {
const suite = new Benchmark.Suite;
const polorizer = new Polorizer();
polorizer.polorize(mixedObject, schema);
polorizer.polorize(mixedObject, structSchema);
const wire = polorizer.bytes();


suite.add('Polorize', () => {
const polorizer = new Polorizer();
polorizer.polorize(mixedObject, schema);
polorizer.polorize(mixedObject, structSchema);
}).add('Depolorize', () => {
const depolorizer = new Depolorizer(wire);
depolorizer.depolorize(schema);
depolorizer.depolorize(structSchema);
}).on('cycle', (event) => {
console.log(String(event.target));
}).run({ 'async': true });
})();

(():void => {
const suite = new Benchmark.Suite;
const doc = documentEncode(mixedObject, schema);
const doc = documentEncode(mixedObject, structSchema);
const docWire = doc.bytes();

suite.add('Document Encode', () => {
documentEncode(mixedObject, schema);
documentEncode(mixedObject, structSchema);
}).add('Decode To Document', () => {
const deplorizer = new Depolorizer(docWire);
deplorizer.depolorizeDocument();
}).add('Decode To Struct', () => {
const deplorizer = new Depolorizer(docWire);
deplorizer.depolorize(schema);
deplorizer.depolorize(structSchema);
}).on('cycle', (event) => {
console.log(String(event.target));
}).run({ 'async': true });
Expand Down
Loading