Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript declarations #15

Open
prk3 opened this issue Mar 25, 2020 · 0 comments
Open

Typescript declarations #15

prk3 opened this issue Mar 25, 2020 · 0 comments

Comments

@prk3
Copy link

prk3 commented Mar 25, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant