当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript bson.BSON类代码示例

本文整理汇总了TypeScript中bson.BSON的典型用法代码示例。如果您正苦于以下问题:TypeScript BSON类的具体用法?TypeScript BSON怎么用?TypeScript BSON使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了BSON类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: describe

describe("Custom", function () {
  /**
   * Represents a complex number.
   * It only deals with complex number with small unsigned integer cartesian components for
   * simplicity.
   */
  class Complex {
    readonly real: number;
    readonly imaginary: number;

    constructor(real: number, imaginary: number) {
      this.real = real;
      this.imaginary = imaginary;
      Object.freeze(this);
    }

    static fromString(input: string): Complex {
      const realMatch: RegExpExecArray | null = /^(\d+)(?:\s*\+\s*\d+j)?$/.exec(input);
      const imaginaryMatch: RegExpExecArray | null = /^(?:\d+\s*\+\s*)?(\d+)j$/.exec(input);
      if (realMatch === null && imaginaryMatch === null) {
        throw new Incident("InvalidInput", {input});
      }
      const real: number = realMatch !== null ? parseInt(realMatch[1], 10) : 0;
      const imaginary: number = imaginaryMatch !== null ? parseInt(imaginaryMatch[1], 10) : 0;
      return new Complex(real, imaginary);
    }

    toString(): string {
      const parts: string[] = [];
      if (this.real !== 0) {
        parts.push(this.real.toString(10));
      }
      if (this.imaginary !== 0) {
        parts.push(`${this.imaginary.toString(10)}j`);
      }
      // tslint:disable-next-line:strict-boolean-expressions
      return parts.join(" + ") || "0";
    }
  }

  const complexType: CustomType<Complex> = new CustomType({
    read<R>(reader: Reader<R>, raw: R): Complex {
      return reader.readString(raw, readVisitor({
        fromString: (input: string): Complex => {
          return Complex.fromString(input);
        },
        fromFloat64: (input: number): Complex => {
          return new Complex(input, 0);
        },
      }));
    },
    write<W>(writer: Writer<W>, value: Complex): W {
      return writer.writeString(value.toString());
    },
    testError(value: Complex): Error | undefined {
      if (!(value instanceof Complex)) {
        return createInvalidTypeError("Complex", value);
      }
      return undefined;
    },
    equals(value1: Complex, value2: Complex): boolean {
      return value1.real === value2.real && value1.imaginary === value2.imaginary;
    },
    clone(value: Complex): Complex {
      return new Complex(value.real, value.imaginary);
    },
  });

  const bsonSerializer: bson.BSON = new bson.BSON();

  const items: TypedValue[] = [
    {
      name: "Complex {real: 0, imaginary: 0}",
      value: new Complex(0, 0),
      valid: true,
      output: {
        bson: bsonSerializer.serialize({_: "0"}),
        json: "\"0\"",
        qs: "_=0",
      },
    },
    {
      name: "Complex {real: 1, imaginary: 0}",
      value: new Complex(1, 0),
      valid: true,
      output: {
        bson: bsonSerializer.serialize({_: "1"}),
        json: "\"1\"",
        qs: "_=1",
      },
    },
    {
      name: "Complex {real: 0, imaginary: 2}",
      value: new Complex(0, 2),
      valid: true,
      output: {
        bson: bsonSerializer.serialize({_: "2j"}),
        json: "\"2j\"",
        qs: "_=2j",
      },
//.........这里部分代码省略.........
开发者ID:demurgos,项目名称:via-type,代码行数:101,代码来源:custom.spec.ts

示例2:

import * as bson from 'bson';

// enable hex string caching
bson.ObjectID.cacheHexString = true

let BSON = new bson.BSON();
let Long = bson.Long;

let doc = { long: Long.fromNumber(100) }

// Serialize a document
let data = BSON.serialize(doc, false, true, false);
console.log("data:", data);

// Deserialize the resulting Buffer
let doc_2 = BSON.deserialize(data);
console.log("doc_2:", doc_2);

BSON = new bson.BSON();
data = BSON.serialize(doc);
doc_2 = BSON.deserialize(data);


// Calculate Object Size
BSON = new bson.BSON();
console.log("Calculated Object size - no options object:", BSON.calculateObjectSize(doc));
console.log("Calculated Object size - empty options object:", BSON.calculateObjectSize(doc, {}));
console.log("Calculated Object size - custom options object:", BSON.calculateObjectSize(doc, { ignoreUndefined: false, serializeFunctions: true }));
开发者ID:ErikSchierboom,项目名称:DefinitelyTyped,代码行数:28,代码来源:bson-tests.ts

示例3:

import * as bson from 'bson';

let BSON = new bson.BSON();
let Long = bson.Long;

let doc = {long: Long.fromNumber(100)}

// Serialize a document
let data = BSON.serialize(doc, false, true, false);
console.log("data:", data);

// Deserialize the resulting Buffer
let doc_2 = BSON.deserialize(data);
console.log("doc_2:", doc_2);

BSON = new bson.BSON();
data = BSON.serialize(doc);
doc_2 = BSON.deserialize(data);
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:18,代码来源:bson-tests.ts

示例4: function

describe("SimpleEnum: rename KebabCase", function () {
  enum Node {
    Expression,
    BinaryOperator,
    BlockStatement,
  }

  const $Node: TsEnumType<Node> = new TsEnumType(() => ({enum: Node, changeCase: CaseStyle.KebabCase}));

  const items: TypedValue[] = [
    {
      name: "Node.Expression",
      value: Node.Expression,
      valid: true,
      output: {
        bson: bsonSerializer.serialize({_: "expression"}),
        json: "\"expression\"",
        qs: "_=expression",
      },
    },
    {
      name: "Node.BinaryOperator",
      value: Node.BinaryOperator,
      valid: true,
      output: {
        bson: bsonSerializer.serialize({_: "binary-operator"}),
        json: "\"binary-operator\"",
        qs: "_=binary-operator",
      },
    },
    {
      name: "Node.BlockStatement",
      value: Node.BlockStatement,
      valid: true,
      output: {
        bson: bsonSerializer.serialize({_: "block-statement"}),
        json: "\"block-statement\"",
        qs: "_=block-statement",
      },
    },
    {
      name: "0",
      value: 0,
      valid: true,
      output: {
        bson: bsonSerializer.serialize({_: "expression"}),
        json: "\"expression\"",
        qs: "_=expression",
      },
    },
    {
      name: "1",
      value: 1,
      valid: true,
      output: {
        bson: bsonSerializer.serialize({_: "binary-operator"}),
        json: "\"binary-operator\"",
        qs: "_=binary-operator",
      },
    },
    {
      name: "2",
      value: 2,
      valid: true,
      output: {
        bson: bsonSerializer.serialize({_: "block-statement"}),
        json: "\"block-statement\"",
        qs: "_=block-statement",
      },
    },
  ];

  runTests($Node, items);
});
开发者ID:demurgos,项目名称:via-type,代码行数:74,代码来源:ts-enum.spec.ts


注:本文中的bson.BSON类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。