Skip to content

Typescript declarations #15

@prk3

Description

@prk3

I wrote a type declaration file for jot, because I needed it for my collaboration framework project. I have not tested all operations and only those described in the documentation have declarations. Nevertheless, it might be worth adding index.d.ts to the repo in case someone wants to use the library with Typescript.

Here are the definitions:

declare module 'jot' {

  type Json = null | boolean | number | string | { [key: string]: Json } | Json[];
  type Document = null | boolean | number | string | { [key: string]: Document } | Document[];

  interface Operation {
    inspect(): string;
    isNoOp(): boolean;
    apply(document: Document): Document;
    simplify(): Operation;
    drilldown(index_or_key: number | string): Operation;
    inverse(document: Document): Operation;
    compose(other: Operation): Operation;
    rebase(other: Operation): Operation | null;
    rebase(other: Operation, options: { document: Document; }): Operation;
    toJSON(): Json;
    serialize(): string;
  }

  function opFromJSON(data: Json): Operation;
  function deserialize(string: string): Operation;

  var SET: {
    new(new_value: Document): Operation;
  };

  var LIST: {
    new(operations: Operation[]): Operation;
  };

  var MATH: {
    new(op: 'and' | 'or' | 'xor' | 'add' | 'mult', value: number): Operation;
    new(op: 'and' | 'or' | 'xor', value: boolean): Operation;
    new(op: 'rot', value: [number, number]): Operation;
    new(op: 'not', value: null): Operation;
  };

  var SPLICE: {
    new(index: number, length: number, new_value: Document): Operation;
  };

  var ATINDEX: {
    new(index: number, operation: Operation): Operation;
    new(operations: { [index: number]: Operation }): Operation;
  };

  var MAP: {
    new(operation: Operation): Operation;
  };

  var PUT: {
    new(key: string | number, value: Document): Operation;
  };

  var REM: {
    new(key: string | number): Operation;
  };

  var APPLY: {
    new(key: string | number, operation: Operation): Operation;
    new(operations: { [key: string]: Operation, [key: number]: Operation }): Operation;
  };

  var COPY: {
    new(locations: [string, string][]): Operation;
  };
}

Edit: I corrected declarations on 2020-04-04.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions