JoshData / jot

JSON Operational Transformation (JOT)
355 stars 34 forks source link

Typescript declarations #15

Open prk3 opened 4 years ago

prk3 commented 4 years ago

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.