How to use the tiny-decoders.map 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 { a: string; b: number; }
autoRecord({ a: string, b: number })(undefined);
// $ExpectType { a: string; b: number; }
autoRecord({ a: string, b: number })(undefined);
// $ExpectType { a: string; b: number; c: boolean | undefined; }
autoRecord({ a: string, b: number, c: optional(boolean) })(undefined);
// $ExpectType string
deep([], string)(undefined);
// $ExpectType string | undefined
optional(string)(undefined);
// $ExpectType string
optional(string, "default")(undefined);
// $ExpectType string | null
optional(string, null)(undefined);
// $ExpectType string
map(string, string)(undefined);
// $ExpectType string | number
either(string, number)(undefined);
// $ExpectType string | number | boolean | { readonly [key: string]: unknown; }
either(either(boolean, string), either(number, mixedDict))(undefined);
// $ExpectType string
lazy(() => string)(undefined);

// $ExpectError
boolean(undefined, []);
// $ExpectError
number(undefined, []);
// $ExpectError
string(undefined, []);
// $ExpectError
mixedArray(undefined, []);
// $ExpectError
github lydell / LinkHints / src / shared / keyboard.js View on Github external
part !== ""
        ? // Modifiers.
          { ...shortcut, [part]: true }
        : shortcut,
    { ...EMPTY_SHORTCUT }
  );
}

export type KeyboardMapping = {
  shortcut: Shortcut,
  action: KeyboardAction,
};

export const decodeKeyboardMapping: Decoder = autoRecord({
  shortcut: decodeShortcut,
  action: map(string, decodeKeyboardAction),
});

export const decodeKeyboardMappingWithModifiers: Decoder = autoRecord(
  {
    shortcut: map(decodeShortcut, requireModifier),
    action: map(string, decodeKeyboardAction),
  }
);

export type KeyboardModeBackground =
  | { type: "FromHintsState" }
  | { type: "PreventOverTyping", sinceTimestamp: number }
  | { type: "Capture" };

export type KeyboardModeWorker =
  | "Normal"
github lydell / LinkHints / src / options / Program.js View on Github external
async restorePosition() {
    if (!PROD) {
      if (this.hasRestoredPosition) {
        return;
      }
      this.hasRestoredPosition = true;
      const recordProps = {
        expandedPerfTabIds: optional(
          map(array(string), ids =>
            ids.filter(id => ({}.hasOwnProperty.call(this.state.perf, id)))
          ),
          ([]: Array)
        ),
        expandedPerf: optional(boolean, false),
        expandedDebug: optional(boolean, false),
        scrollY: optional(number, 0),
      };
      const data = await browser.storage.local.get(Object.keys(recordProps));
      const decoder = autoRecord(recordProps);
      const { scrollY, expandedPerfTabIds, ...state } = decoder(data);
      this.setState({ ...state, expandedPerfTabIds }, () => {
        window.scrollTo(0, scrollY);
      });
    }
  }
github lydell / LinkHints / src / shared / options.js View on Github external
return record(field => ({
    chars: field("chars", map(string, validateChars), {
      default: defaults.chars,
    }),
    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,
github lydell / LinkHints / src / shared / tweakable.js View on Github external
break;
          }

          case "StringSet": {
            const decoded = decodeStringSet(string)(value);
            mapping[key] = {
              type: "StringSet",
              value: decoded,
            };
            changed[key] = !equalStringSets(decoded, original.value);
            break;
          }

          case "ElementTypeSet": {
            const decoded: Set = decodeStringSet(
              map(string, decodeElementType)
            )(value);
            mapping[key] = {
              type: "ElementTypeSet",
              value: decoded,
            };
            changed[key] = !equalStringSets(
              new Set(decoded),
              new Set(original.value)
            );
            break;
          }

          case "SelectorString": {
            const decoded = map(string, val => {
              document.querySelector(val);
              return val;
github lydell / LinkHints / src / shared / keyboard.js View on Github external
);
}

export type KeyboardMapping = {
  shortcut: Shortcut,
  action: KeyboardAction,
};

export const decodeKeyboardMapping: Decoder = autoRecord({
  shortcut: decodeShortcut,
  action: map(string, decodeKeyboardAction),
});

export const decodeKeyboardMappingWithModifiers: Decoder = autoRecord(
  {
    shortcut: map(decodeShortcut, requireModifier),
    action: map(string, decodeKeyboardAction),
  }
);

export type KeyboardModeBackground =
  | { type: "FromHintsState" }
  | { type: "PreventOverTyping", sinceTimestamp: number }
  | { type: "Capture" };

export type KeyboardModeWorker =
  | "Normal"
  | "Hints"
  | "PreventOverTyping"
  | "Capture";

export type HintsMode =
github lydell / LinkHints / src / shared / options.js View on Github external
return record(field => ({
    chars: field("chars", map(string, validateChars), {
      default: defaults.chars,
    }),
    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"),
github lydell / LinkHints / src / shared / hints.js View on Github external
default:
      throw new TypeError(`Invalid ElementType: ${repr(type)}`);
  }
}

export function decodeElementTypesConstants(type: string): ElementTypes {
  switch (type) {
    case "selectable":
      return type;
    default:
      throw new TypeError(`Invalid ElementTypes constant: ${repr(type)}`);
  }
}

export const decodeElementTypes: Decoder = either(
  map(string, decodeElementTypesConstants),
  array(map(string, decodeElementType))
);

export type Point = {
  x: number,
  y: number,
  align: "left" | "right",
  debug: string,
};

export type HintMeasurements = {
  ...Point,
  maxX: number,
  weight: number,
};
github lydell / LinkHints / src / shared / tweakable.js View on Github external
const decoded: Set = decodeStringSet(
              map(string, decodeElementType)
            )(value);
            mapping[key] = {
              type: "ElementTypeSet",
              value: decoded,
            };
            changed[key] = !equalStringSets(
              new Set(decoded),
              new Set(original.value)
            );
            break;
          }

          case "SelectorString": {
            const decoded = map(string, val => {
              document.querySelector(val);
              return val;
            })(value);
            mapping[key] = {
              type: "SelectorString",
              value: decoded,
            };
            changed[key] = decoded !== original.value;
            break;
          }

          default:
            unreachable(original.type, original);
        }
      } catch (error) {
        errors[key] = error.message;