How to use the tiny-decoders.dict function in tiny-decoders

To help you get started, we’ve selected a few tiny-decoders examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lydell / tiny-decoders / typescript / decoders.ts View on Github external
// $ExpectType true
constant(true)(undefined);
// $ExpectType false
constant(false)(undefined);
// $ExpectType 0
constant(0)(undefined);
// $ExpectType "const"
constant("const")(undefined);
// $ExpectType undefined
constant(undefined)(undefined);
// $ExpectType null
constant(null)(undefined);
// $ExpectType string[]
array(string)(undefined);
// $ExpectType { [key: string]: string; }
dict(string)(undefined);
// $ExpectType string
record(() => "")(undefined);
// $ExpectType { a: string; }
record(field => ({ a: field("a", string) }))(undefined);
// $ExpectType string
tuple(() => "")(undefined);
// $ExpectType string[]
tuple(item => [item(0, string)])(undefined);
// $ExpectType [string]
tuple<[string]>(item => [item(0, string)])(undefined);
// $ExpectType [string, boolean]
pair(string, boolean)(undefined);
// $ExpectType [string, boolean, boolean]
triple(string, boolean, boolean)(undefined);
// $ExpectType {}
autoRecord<{}>({})(undefined);
github lydell / tiny-decoders / typescript / decoders.ts View on Github external
array(string, "throw");
array(string, "skip");
array(string, { default: "" });
array(string, { default: [1] });
// Wrong mode:
// $ExpectError
array(string, "nope");

dict(string);
dict(string, "throw");
dict(string, "skip");
dict(string, { default: "" });
dict(string, { default: [1] });
// Wrong mode:
// $ExpectError
dict(string, "nope");

// Accidentally passed an object instead of a callback:
record({
  // $ExpectError
  a: string,
});
record(field => field("", string));
record(field => field("", string, "throw"));
record(field => field("", string, { default: "" }));
record(field => field("", string, { default: null }));
// Wrong key type:
// $ExpectError
record(field => field(0, string));
// Wrong order:
// $ExpectError
record(field => field(string, ""));
github lydell / tiny-decoders / typescript / decoders.ts View on Github external
// $ExpectError
constant({ type: "user" });
// Accidentally passed a decoder:
// $ExpectError
constant(string);

array(string);
array(string, "throw");
array(string, "skip");
array(string, { default: "" });
array(string, { default: [1] });
// Wrong mode:
// $ExpectError
array(string, "nope");

dict(string);
dict(string, "throw");
dict(string, "skip");
dict(string, { default: "" });
dict(string, { default: [1] });
// Wrong mode:
// $ExpectError
dict(string, "nope");

// Accidentally passed an object instead of a callback:
record({
  // $ExpectError
  a: string,
});
record(field => field("", string));
record(field => field("", string, "throw"));
record(field => field("", string, { default: "" }));
github lydell / tiny-decoders / typescript / decoders.ts View on Github external
constant({ type: "user" });
// Accidentally passed a decoder:
// $ExpectError
constant(string);

array(string);
array(string, "throw");
array(string, "skip");
array(string, { default: "" });
array(string, { default: [1] });
// Wrong mode:
// $ExpectError
array(string, "nope");

dict(string);
dict(string, "throw");
dict(string, "skip");
dict(string, { default: "" });
dict(string, { default: [1] });
// Wrong mode:
// $ExpectError
dict(string, "nope");

// Accidentally passed an object instead of a callback:
record({
  // $ExpectError
  a: string,
});
record(field => field("", string));
record(field => field("", string, "throw"));
record(field => field("", string, { default: "" }));
record(field => field("", string, { default: null }));
github lydell / tiny-decoders / typescript / decoders.ts View on Github external
// Accidentally passed a decoder:
// $ExpectError
constant(string);

array(string);
array(string, "throw");
array(string, "skip");
array(string, { default: "" });
array(string, { default: [1] });
// Wrong mode:
// $ExpectError
array(string, "nope");

dict(string);
dict(string, "throw");
dict(string, "skip");
dict(string, { default: "" });
dict(string, { default: [1] });
// Wrong mode:
// $ExpectError
dict(string, "nope");

// Accidentally passed an object instead of a callback:
record({
  // $ExpectError
  a: string,
});
record(field => field("", string));
record(field => field("", string, "throw"));
record(field => field("", string, { default: "" }));
record(field => field("", string, { default: null }));
// Wrong key type:
github lydell / LinkHints / src / shared / perf.js View on Github external
renderDurations: Durations,
}>;

export const decodePerf: Decoder = array(
  autoRecord({
    timeToFirstPaint: number,
    timeToLastPaint: number,
    topDurations: decodeDurations,
    collectStats: array(decodeStats),
    renderDurations: decodeDurations,
  })
);

export type TabsPerf = { [tabId: string]: Perf, ... };

export const decodeTabsPerf: Decoder = dict(decodePerf);

export class TimeTracker {
  _durations: Durations = [];
  _current: ?{ label: string, timestamp: number } = undefined;

  start(label: string) {
    this.stop();

    this._current = {
      label,
      timestamp: Date.now(),
    };
  }

  stop() {
    const current = this._current;
github lydell / LinkHints / src / shared / options.js View on Github external
autoActivate: field("autoActivate", boolean, {
      default: defaults.autoActivate,
    }),
    overTypingDuration: field("overTypingDuration", decodeUnsignedInt, {
      default: defaults.overTypingDuration,
    }),
    css: field("css", string, {
      default: defaults.css,
    }),
    logLevel: field("logLevel", map(string, decodeLogLevel), {
      default: defaults.logLevel,
    }),
    useKeyTranslations: field("useKeyTranslations", boolean, {
      default: defaults.useKeyTranslations,
    }),
    keyTranslations: field("keyTranslations", dict(decodeKeyPair, "skip"), {
      default: defaults.keyTranslations,
    }),
    normalKeyboardShortcuts: field(
      "normalKeyboardShortcuts",
      array(decodeKeyboardMappingWithModifiers, "skip"),
      { default: defaults.normalKeyboardShortcuts }
    ),
    hintsKeyboardShortcuts: field(
      "hintsKeyboardShortcuts",
      array(decodeKeyboardMapping, "skip"),
      { default: defaults.hintsKeyboardShortcuts }
    ),
  }));
}