Files
dolt/samples/js/encode-perf-rig/src/string-encoder.js
Erik Arvidsson 6178251012 Update to Flow 0.30.0 (#2337)
This requires all parameterized types to have type params. Fortunately
one can use `T<any>` which has the same behavior as the old `T` syntax.

We should tighten the types further after this but this unblocks us.

Fixes #2301
2016-08-11 11:27:41 -07:00

25 lines
658 B
JavaScript

// Copyright 2016 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
// @flow
export class StringEncoderDecoder {
// write n to buf, return number of bytes written
encode(buf: Buffer, n: number): number {
if (n < 1e20) {
// $FlowIssue: Buffer.prototype.write returns a number
return buf.write(n.toString(10));
}
// $FlowIssue: Buffer.prototype.write returns a number
return buf.write(n.toExponential());
}
// read from buf to return number
decode(buf: Buffer): number {
const s = buf.toString();
return parseFloat(s);
}
}