+2
@@ -0,0 +1,2 @@
|
||||
// Conveniently represents flow's "Maybe" type https://flow.org/en/docs/types/maybe/
|
||||
export type Maybe<T> = null | undefined | T;
|
||||
+1
@@ -0,0 +1 @@
|
||||
"use strict";
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// @flow strict
|
||||
export type ObjMap<T> = { [key: string]: T, __proto__: null, ... };
|
||||
export type ObjMapLike<T> = ObjMap<T> | { [key: string]: T, ... };
|
||||
|
||||
export type ReadOnlyObjMap<T> = { +[key: string]: T, __proto__: null, ... };
|
||||
export type ReadOnlyObjMapLike<T> =
|
||||
| ReadOnlyObjMap<T>
|
||||
| { +[key: string]: T, ... };
|
||||
+1
@@ -0,0 +1 @@
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
export interface Path {
|
||||
prev: Path | undefined;
|
||||
key: string | number;
|
||||
typename: string | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a Path and a key, return a new Path containing the new key.
|
||||
*/
|
||||
export function addPath(
|
||||
prev: Path | undefined,
|
||||
key: string | number,
|
||||
typename: string | undefined,
|
||||
): Path;
|
||||
|
||||
/**
|
||||
* Given a Path, return an Array of the path keys.
|
||||
*/
|
||||
export function pathToArray(path: Path): Array<string | number>;
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.addPath = addPath;
|
||||
exports.pathToArray = pathToArray;
|
||||
|
||||
/**
|
||||
* Given a Path and a key, return a new Path containing the new key.
|
||||
*/
|
||||
function addPath(prev, key, typename) {
|
||||
return {
|
||||
prev: prev,
|
||||
key: key,
|
||||
typename: typename
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Given a Path, return an Array of the path keys.
|
||||
*/
|
||||
|
||||
|
||||
function pathToArray(path) {
|
||||
var flattened = [];
|
||||
var curr = path;
|
||||
|
||||
while (curr) {
|
||||
flattened.push(curr.key);
|
||||
curr = curr.prev;
|
||||
}
|
||||
|
||||
return flattened.reverse();
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
// @flow strict
|
||||
export type Path = {|
|
||||
+prev: Path | void,
|
||||
+key: string | number,
|
||||
+typename: string | void,
|
||||
|};
|
||||
|
||||
/**
|
||||
* Given a Path and a key, return a new Path containing the new key.
|
||||
*/
|
||||
export function addPath(
|
||||
prev: $ReadOnly<Path> | void,
|
||||
key: string | number,
|
||||
typename: string | void,
|
||||
): Path {
|
||||
return { prev, key, typename };
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a Path, return an Array of the path keys.
|
||||
*/
|
||||
export function pathToArray(path: ?$ReadOnly<Path>): Array<string | number> {
|
||||
const flattened = [];
|
||||
let curr = path;
|
||||
while (curr) {
|
||||
flattened.push(curr.key);
|
||||
curr = curr.prev;
|
||||
}
|
||||
return flattened.reverse();
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Given a Path and a key, return a new Path containing the new key.
|
||||
*/
|
||||
export function addPath(prev, key, typename) {
|
||||
return {
|
||||
prev: prev,
|
||||
key: key,
|
||||
typename: typename
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Given a Path, return an Array of the path keys.
|
||||
*/
|
||||
|
||||
export function pathToArray(path) {
|
||||
var flattened = [];
|
||||
var curr = path;
|
||||
|
||||
while (curr) {
|
||||
flattened.push(curr.key);
|
||||
curr = curr.prev;
|
||||
}
|
||||
|
||||
return flattened.reverse();
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export type PromiseOrValue<T> = Promise<T> | T;
|
||||
+1
@@ -0,0 +1 @@
|
||||
"use strict";
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
// @flow strict
|
||||
export type PromiseOrValue<+T> = Promise<T> | T;
|
||||
+1
@@ -0,0 +1 @@
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = defineInspect;
|
||||
|
||||
var _invariant = _interopRequireDefault(require("./invariant.js"));
|
||||
|
||||
var _nodejsCustomInspectSymbol = _interopRequireDefault(require("./nodejsCustomInspectSymbol.js"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
/**
|
||||
* The `defineInspect()` function defines `inspect()` prototype method as alias of `toJSON`
|
||||
*/
|
||||
function defineInspect(classObject) {
|
||||
var fn = classObject.prototype.toJSON;
|
||||
typeof fn === 'function' || (0, _invariant.default)(0);
|
||||
classObject.prototype.inspect = fn; // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2317')
|
||||
|
||||
if (_nodejsCustomInspectSymbol.default) {
|
||||
classObject.prototype[_nodejsCustomInspectSymbol.default] = fn;
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// @flow strict
|
||||
import invariant from './invariant';
|
||||
import nodejsCustomInspectSymbol from './nodejsCustomInspectSymbol';
|
||||
|
||||
/**
|
||||
* The `defineInspect()` function defines `inspect()` prototype method as alias of `toJSON`
|
||||
*/
|
||||
export default function defineInspect(
|
||||
classObject: Class<any> | ((...args: Array<any>) => mixed),
|
||||
): void {
|
||||
const fn = classObject.prototype.toJSON;
|
||||
invariant(typeof fn === 'function');
|
||||
|
||||
classObject.prototype.inspect = fn;
|
||||
|
||||
// istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2317')
|
||||
if (nodejsCustomInspectSymbol) {
|
||||
classObject.prototype[nodejsCustomInspectSymbol] = fn;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import invariant from "./invariant.mjs";
|
||||
import nodejsCustomInspectSymbol from "./nodejsCustomInspectSymbol.mjs";
|
||||
/**
|
||||
* The `defineInspect()` function defines `inspect()` prototype method as alias of `toJSON`
|
||||
*/
|
||||
|
||||
export default function defineInspect(classObject) {
|
||||
var fn = classObject.prototype.toJSON;
|
||||
typeof fn === 'function' || invariant(0);
|
||||
classObject.prototype.inspect = fn; // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2317')
|
||||
|
||||
if (nodejsCustomInspectSymbol) {
|
||||
classObject.prototype[nodejsCustomInspectSymbol] = fn;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = devAssert;
|
||||
|
||||
function devAssert(condition, message) {
|
||||
var booleanCondition = Boolean(condition); // istanbul ignore else (See transformation done in './resources/inlineInvariant.js')
|
||||
|
||||
if (!booleanCondition) {
|
||||
throw new Error(message);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// @flow strict
|
||||
export default function devAssert(condition: mixed, message: string): void {
|
||||
const booleanCondition = Boolean(condition);
|
||||
// istanbul ignore else (See transformation done in './resources/inlineInvariant.js')
|
||||
if (!booleanCondition) {
|
||||
throw new Error(message);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
export default function devAssert(condition, message) {
|
||||
var booleanCondition = Boolean(condition); // istanbul ignore else (See transformation done in './resources/inlineInvariant.js')
|
||||
|
||||
if (!booleanCondition) {
|
||||
throw new Error(message);
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = didYouMean;
|
||||
var MAX_SUGGESTIONS = 5;
|
||||
/**
|
||||
* Given [ A, B, C ] return ' Did you mean A, B, or C?'.
|
||||
*/
|
||||
|
||||
// eslint-disable-next-line no-redeclare
|
||||
function didYouMean(firstArg, secondArg) {
|
||||
var _ref = typeof firstArg === 'string' ? [firstArg, secondArg] : [undefined, firstArg],
|
||||
subMessage = _ref[0],
|
||||
suggestionsArg = _ref[1];
|
||||
|
||||
var message = ' Did you mean ';
|
||||
|
||||
if (subMessage) {
|
||||
message += subMessage + ' ';
|
||||
}
|
||||
|
||||
var suggestions = suggestionsArg.map(function (x) {
|
||||
return "\"".concat(x, "\"");
|
||||
});
|
||||
|
||||
switch (suggestions.length) {
|
||||
case 0:
|
||||
return '';
|
||||
|
||||
case 1:
|
||||
return message + suggestions[0] + '?';
|
||||
|
||||
case 2:
|
||||
return message + suggestions[0] + ' or ' + suggestions[1] + '?';
|
||||
}
|
||||
|
||||
var selected = suggestions.slice(0, MAX_SUGGESTIONS);
|
||||
var lastItem = selected.pop();
|
||||
return message + selected.join(', ') + ', or ' + lastItem + '?';
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
// @flow strict
|
||||
const MAX_SUGGESTIONS = 5;
|
||||
|
||||
/**
|
||||
* Given [ A, B, C ] return ' Did you mean A, B, or C?'.
|
||||
*/
|
||||
declare function didYouMean(suggestions: $ReadOnlyArray<string>): string;
|
||||
// eslint-disable-next-line no-redeclare
|
||||
declare function didYouMean(
|
||||
subMessage: string,
|
||||
suggestions: $ReadOnlyArray<string>,
|
||||
): string;
|
||||
|
||||
// eslint-disable-next-line no-redeclare
|
||||
export default function didYouMean(firstArg, secondArg) {
|
||||
const [subMessage, suggestionsArg] =
|
||||
typeof firstArg === 'string'
|
||||
? [firstArg, secondArg]
|
||||
: [undefined, firstArg];
|
||||
|
||||
let message = ' Did you mean ';
|
||||
if (subMessage) {
|
||||
message += subMessage + ' ';
|
||||
}
|
||||
|
||||
const suggestions = suggestionsArg.map((x) => `"${x}"`);
|
||||
switch (suggestions.length) {
|
||||
case 0:
|
||||
return '';
|
||||
case 1:
|
||||
return message + suggestions[0] + '?';
|
||||
case 2:
|
||||
return message + suggestions[0] + ' or ' + suggestions[1] + '?';
|
||||
}
|
||||
|
||||
const selected = suggestions.slice(0, MAX_SUGGESTIONS);
|
||||
const lastItem = selected.pop();
|
||||
return message + selected.join(', ') + ', or ' + lastItem + '?';
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
var MAX_SUGGESTIONS = 5;
|
||||
/**
|
||||
* Given [ A, B, C ] return ' Did you mean A, B, or C?'.
|
||||
*/
|
||||
|
||||
// eslint-disable-next-line no-redeclare
|
||||
export default function didYouMean(firstArg, secondArg) {
|
||||
var _ref = typeof firstArg === 'string' ? [firstArg, secondArg] : [undefined, firstArg],
|
||||
subMessage = _ref[0],
|
||||
suggestionsArg = _ref[1];
|
||||
|
||||
var message = ' Did you mean ';
|
||||
|
||||
if (subMessage) {
|
||||
message += subMessage + ' ';
|
||||
}
|
||||
|
||||
var suggestions = suggestionsArg.map(function (x) {
|
||||
return "\"".concat(x, "\"");
|
||||
});
|
||||
|
||||
switch (suggestions.length) {
|
||||
case 0:
|
||||
return '';
|
||||
|
||||
case 1:
|
||||
return message + suggestions[0] + '?';
|
||||
|
||||
case 2:
|
||||
return message + suggestions[0] + ' or ' + suggestions[1] + '?';
|
||||
}
|
||||
|
||||
var selected = suggestions.slice(0, MAX_SUGGESTIONS);
|
||||
var lastItem = selected.pop();
|
||||
return message + selected.join(', ') + ', or ' + lastItem + '?';
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = identityFunc;
|
||||
|
||||
/**
|
||||
* Returns the first argument it receives.
|
||||
*/
|
||||
function identityFunc(x) {
|
||||
return x;
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// @flow strict
|
||||
/**
|
||||
* Returns the first argument it receives.
|
||||
*/
|
||||
export default function identityFunc<T>(x: T): T {
|
||||
return x;
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* Returns the first argument it receives.
|
||||
*/
|
||||
export default function identityFunc(x) {
|
||||
return x;
|
||||
}
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = inspect;
|
||||
|
||||
var _nodejsCustomInspectSymbol = _interopRequireDefault(require("./nodejsCustomInspectSymbol.js"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
||||
|
||||
var MAX_ARRAY_LENGTH = 10;
|
||||
var MAX_RECURSIVE_DEPTH = 2;
|
||||
/**
|
||||
* Used to print values in error messages.
|
||||
*/
|
||||
|
||||
function inspect(value) {
|
||||
return formatValue(value, []);
|
||||
}
|
||||
|
||||
function formatValue(value, seenValues) {
|
||||
switch (_typeof(value)) {
|
||||
case 'string':
|
||||
return JSON.stringify(value);
|
||||
|
||||
case 'function':
|
||||
return value.name ? "[function ".concat(value.name, "]") : '[function]';
|
||||
|
||||
case 'object':
|
||||
if (value === null) {
|
||||
return 'null';
|
||||
}
|
||||
|
||||
return formatObjectValue(value, seenValues);
|
||||
|
||||
default:
|
||||
return String(value);
|
||||
}
|
||||
}
|
||||
|
||||
function formatObjectValue(value, previouslySeenValues) {
|
||||
if (previouslySeenValues.indexOf(value) !== -1) {
|
||||
return '[Circular]';
|
||||
}
|
||||
|
||||
var seenValues = [].concat(previouslySeenValues, [value]);
|
||||
var customInspectFn = getCustomFn(value);
|
||||
|
||||
if (customInspectFn !== undefined) {
|
||||
var customValue = customInspectFn.call(value); // check for infinite recursion
|
||||
|
||||
if (customValue !== value) {
|
||||
return typeof customValue === 'string' ? customValue : formatValue(customValue, seenValues);
|
||||
}
|
||||
} else if (Array.isArray(value)) {
|
||||
return formatArray(value, seenValues);
|
||||
}
|
||||
|
||||
return formatObject(value, seenValues);
|
||||
}
|
||||
|
||||
function formatObject(object, seenValues) {
|
||||
var keys = Object.keys(object);
|
||||
|
||||
if (keys.length === 0) {
|
||||
return '{}';
|
||||
}
|
||||
|
||||
if (seenValues.length > MAX_RECURSIVE_DEPTH) {
|
||||
return '[' + getObjectTag(object) + ']';
|
||||
}
|
||||
|
||||
var properties = keys.map(function (key) {
|
||||
var value = formatValue(object[key], seenValues);
|
||||
return key + ': ' + value;
|
||||
});
|
||||
return '{ ' + properties.join(', ') + ' }';
|
||||
}
|
||||
|
||||
function formatArray(array, seenValues) {
|
||||
if (array.length === 0) {
|
||||
return '[]';
|
||||
}
|
||||
|
||||
if (seenValues.length > MAX_RECURSIVE_DEPTH) {
|
||||
return '[Array]';
|
||||
}
|
||||
|
||||
var len = Math.min(MAX_ARRAY_LENGTH, array.length);
|
||||
var remaining = array.length - len;
|
||||
var items = [];
|
||||
|
||||
for (var i = 0; i < len; ++i) {
|
||||
items.push(formatValue(array[i], seenValues));
|
||||
}
|
||||
|
||||
if (remaining === 1) {
|
||||
items.push('... 1 more item');
|
||||
} else if (remaining > 1) {
|
||||
items.push("... ".concat(remaining, " more items"));
|
||||
}
|
||||
|
||||
return '[' + items.join(', ') + ']';
|
||||
}
|
||||
|
||||
function getCustomFn(object) {
|
||||
var customInspectFn = object[String(_nodejsCustomInspectSymbol.default)];
|
||||
|
||||
if (typeof customInspectFn === 'function') {
|
||||
return customInspectFn;
|
||||
}
|
||||
|
||||
if (typeof object.inspect === 'function') {
|
||||
return object.inspect;
|
||||
}
|
||||
}
|
||||
|
||||
function getObjectTag(object) {
|
||||
var tag = Object.prototype.toString.call(object).replace(/^\[object /, '').replace(/]$/, '');
|
||||
|
||||
if (tag === 'Object' && typeof object.constructor === 'function') {
|
||||
var name = object.constructor.name;
|
||||
|
||||
if (typeof name === 'string' && name !== '') {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
return tag;
|
||||
}
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
// @flow strict
|
||||
/* eslint-disable flowtype/no-weak-types */
|
||||
import nodejsCustomInspectSymbol from './nodejsCustomInspectSymbol';
|
||||
|
||||
const MAX_ARRAY_LENGTH = 10;
|
||||
const MAX_RECURSIVE_DEPTH = 2;
|
||||
|
||||
/**
|
||||
* Used to print values in error messages.
|
||||
*/
|
||||
export default function inspect(value: mixed): string {
|
||||
return formatValue(value, []);
|
||||
}
|
||||
|
||||
function formatValue(value: mixed, seenValues: Array<mixed>): string {
|
||||
switch (typeof value) {
|
||||
case 'string':
|
||||
return JSON.stringify(value);
|
||||
case 'function':
|
||||
return value.name ? `[function ${value.name}]` : '[function]';
|
||||
case 'object':
|
||||
if (value === null) {
|
||||
return 'null';
|
||||
}
|
||||
return formatObjectValue(value, seenValues);
|
||||
default:
|
||||
return String(value);
|
||||
}
|
||||
}
|
||||
|
||||
function formatObjectValue(
|
||||
value: Object,
|
||||
previouslySeenValues: Array<mixed>,
|
||||
): string {
|
||||
if (previouslySeenValues.indexOf(value) !== -1) {
|
||||
return '[Circular]';
|
||||
}
|
||||
|
||||
const seenValues = [...previouslySeenValues, value];
|
||||
const customInspectFn = getCustomFn(value);
|
||||
|
||||
if (customInspectFn !== undefined) {
|
||||
const customValue = customInspectFn.call(value);
|
||||
|
||||
// check for infinite recursion
|
||||
if (customValue !== value) {
|
||||
return typeof customValue === 'string'
|
||||
? customValue
|
||||
: formatValue(customValue, seenValues);
|
||||
}
|
||||
} else if (Array.isArray(value)) {
|
||||
return formatArray(value, seenValues);
|
||||
}
|
||||
|
||||
return formatObject(value, seenValues);
|
||||
}
|
||||
|
||||
function formatObject(object: Object, seenValues: Array<mixed>): string {
|
||||
const keys = Object.keys(object);
|
||||
if (keys.length === 0) {
|
||||
return '{}';
|
||||
}
|
||||
|
||||
if (seenValues.length > MAX_RECURSIVE_DEPTH) {
|
||||
return '[' + getObjectTag(object) + ']';
|
||||
}
|
||||
|
||||
const properties = keys.map((key) => {
|
||||
const value = formatValue(object[key], seenValues);
|
||||
return key + ': ' + value;
|
||||
});
|
||||
|
||||
return '{ ' + properties.join(', ') + ' }';
|
||||
}
|
||||
|
||||
function formatArray(array: Array<mixed>, seenValues: Array<mixed>): string {
|
||||
if (array.length === 0) {
|
||||
return '[]';
|
||||
}
|
||||
|
||||
if (seenValues.length > MAX_RECURSIVE_DEPTH) {
|
||||
return '[Array]';
|
||||
}
|
||||
|
||||
const len = Math.min(MAX_ARRAY_LENGTH, array.length);
|
||||
const remaining = array.length - len;
|
||||
const items = [];
|
||||
|
||||
for (let i = 0; i < len; ++i) {
|
||||
items.push(formatValue(array[i], seenValues));
|
||||
}
|
||||
|
||||
if (remaining === 1) {
|
||||
items.push('... 1 more item');
|
||||
} else if (remaining > 1) {
|
||||
items.push(`... ${remaining} more items`);
|
||||
}
|
||||
|
||||
return '[' + items.join(', ') + ']';
|
||||
}
|
||||
|
||||
function getCustomFn(object: Object) {
|
||||
const customInspectFn = object[String(nodejsCustomInspectSymbol)];
|
||||
|
||||
if (typeof customInspectFn === 'function') {
|
||||
return customInspectFn;
|
||||
}
|
||||
|
||||
if (typeof object.inspect === 'function') {
|
||||
return object.inspect;
|
||||
}
|
||||
}
|
||||
|
||||
function getObjectTag(object: Object): string {
|
||||
const tag = Object.prototype.toString
|
||||
.call(object)
|
||||
.replace(/^\[object /, '')
|
||||
.replace(/]$/, '');
|
||||
|
||||
if (tag === 'Object' && typeof object.constructor === 'function') {
|
||||
const name = object.constructor.name;
|
||||
if (typeof name === 'string' && name !== '') {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
return tag;
|
||||
}
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
||||
|
||||
/* eslint-disable flowtype/no-weak-types */
|
||||
import nodejsCustomInspectSymbol from "./nodejsCustomInspectSymbol.mjs";
|
||||
var MAX_ARRAY_LENGTH = 10;
|
||||
var MAX_RECURSIVE_DEPTH = 2;
|
||||
/**
|
||||
* Used to print values in error messages.
|
||||
*/
|
||||
|
||||
export default function inspect(value) {
|
||||
return formatValue(value, []);
|
||||
}
|
||||
|
||||
function formatValue(value, seenValues) {
|
||||
switch (_typeof(value)) {
|
||||
case 'string':
|
||||
return JSON.stringify(value);
|
||||
|
||||
case 'function':
|
||||
return value.name ? "[function ".concat(value.name, "]") : '[function]';
|
||||
|
||||
case 'object':
|
||||
if (value === null) {
|
||||
return 'null';
|
||||
}
|
||||
|
||||
return formatObjectValue(value, seenValues);
|
||||
|
||||
default:
|
||||
return String(value);
|
||||
}
|
||||
}
|
||||
|
||||
function formatObjectValue(value, previouslySeenValues) {
|
||||
if (previouslySeenValues.indexOf(value) !== -1) {
|
||||
return '[Circular]';
|
||||
}
|
||||
|
||||
var seenValues = [].concat(previouslySeenValues, [value]);
|
||||
var customInspectFn = getCustomFn(value);
|
||||
|
||||
if (customInspectFn !== undefined) {
|
||||
var customValue = customInspectFn.call(value); // check for infinite recursion
|
||||
|
||||
if (customValue !== value) {
|
||||
return typeof customValue === 'string' ? customValue : formatValue(customValue, seenValues);
|
||||
}
|
||||
} else if (Array.isArray(value)) {
|
||||
return formatArray(value, seenValues);
|
||||
}
|
||||
|
||||
return formatObject(value, seenValues);
|
||||
}
|
||||
|
||||
function formatObject(object, seenValues) {
|
||||
var keys = Object.keys(object);
|
||||
|
||||
if (keys.length === 0) {
|
||||
return '{}';
|
||||
}
|
||||
|
||||
if (seenValues.length > MAX_RECURSIVE_DEPTH) {
|
||||
return '[' + getObjectTag(object) + ']';
|
||||
}
|
||||
|
||||
var properties = keys.map(function (key) {
|
||||
var value = formatValue(object[key], seenValues);
|
||||
return key + ': ' + value;
|
||||
});
|
||||
return '{ ' + properties.join(', ') + ' }';
|
||||
}
|
||||
|
||||
function formatArray(array, seenValues) {
|
||||
if (array.length === 0) {
|
||||
return '[]';
|
||||
}
|
||||
|
||||
if (seenValues.length > MAX_RECURSIVE_DEPTH) {
|
||||
return '[Array]';
|
||||
}
|
||||
|
||||
var len = Math.min(MAX_ARRAY_LENGTH, array.length);
|
||||
var remaining = array.length - len;
|
||||
var items = [];
|
||||
|
||||
for (var i = 0; i < len; ++i) {
|
||||
items.push(formatValue(array[i], seenValues));
|
||||
}
|
||||
|
||||
if (remaining === 1) {
|
||||
items.push('... 1 more item');
|
||||
} else if (remaining > 1) {
|
||||
items.push("... ".concat(remaining, " more items"));
|
||||
}
|
||||
|
||||
return '[' + items.join(', ') + ']';
|
||||
}
|
||||
|
||||
function getCustomFn(object) {
|
||||
var customInspectFn = object[String(nodejsCustomInspectSymbol)];
|
||||
|
||||
if (typeof customInspectFn === 'function') {
|
||||
return customInspectFn;
|
||||
}
|
||||
|
||||
if (typeof object.inspect === 'function') {
|
||||
return object.inspect;
|
||||
}
|
||||
}
|
||||
|
||||
function getObjectTag(object) {
|
||||
var tag = Object.prototype.toString.call(object).replace(/^\[object /, '').replace(/]$/, '');
|
||||
|
||||
if (tag === 'Object' && typeof object.constructor === 'function') {
|
||||
var name = object.constructor.name;
|
||||
|
||||
if (typeof name === 'string' && name !== '') {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
return tag;
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
|
||||
var _inspect = _interopRequireDefault(require("./inspect.js"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
||||
|
||||
// See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production
|
||||
// See: https://webpack.js.org/guides/production/
|
||||
var _default = process.env.NODE_ENV === 'production' ? // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
|
||||
// eslint-disable-next-line no-shadow
|
||||
function instanceOf(value, constructor) {
|
||||
return value instanceof constructor;
|
||||
} : // eslint-disable-next-line no-shadow
|
||||
function instanceOf(value, constructor) {
|
||||
if (value instanceof constructor) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_typeof(value) === 'object' && value !== null) {
|
||||
var _value$constructor;
|
||||
|
||||
var className = constructor.prototype[Symbol.toStringTag];
|
||||
var valueClassName = // We still need to support constructor's name to detect conflicts with older versions of this library.
|
||||
Symbol.toStringTag in value ? value[Symbol.toStringTag] : (_value$constructor = value.constructor) === null || _value$constructor === void 0 ? void 0 : _value$constructor.name;
|
||||
|
||||
if (className === valueClassName) {
|
||||
var stringifiedValue = (0, _inspect.default)(value);
|
||||
throw new Error("Cannot use ".concat(className, " \"").concat(stringifiedValue, "\" from another module or realm.\n\nEnsure that there is only one instance of \"graphql\" in the node_modules\ndirectory. If different versions of \"graphql\" are the dependencies of other\nrelied on modules, use \"resolutions\" to ensure only one version is installed.\n\nhttps://yarnpkg.com/en/docs/selective-version-resolutions\n\nDuplicate \"graphql\" modules cannot be used at the same time since different\nversions may have different capabilities and behavior. The data from one\nversion used in the function from another could produce confusing and\nspurious results."));
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
exports.default = _default;
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
// @flow strict
|
||||
import inspect from './inspect';
|
||||
|
||||
/**
|
||||
* A replacement for instanceof which includes an error warning when multi-realm
|
||||
* constructors are detected.
|
||||
*/
|
||||
declare function instanceOf(
|
||||
value: mixed,
|
||||
constructor: mixed,
|
||||
): boolean %checks(value instanceof constructor);
|
||||
|
||||
// See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production
|
||||
// See: https://webpack.js.org/guides/production/
|
||||
export default process.env.NODE_ENV === 'production'
|
||||
? // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
|
||||
// eslint-disable-next-line no-shadow
|
||||
function instanceOf(value: mixed, constructor: mixed): boolean {
|
||||
return value instanceof constructor;
|
||||
}
|
||||
: // eslint-disable-next-line no-shadow
|
||||
function instanceOf(value: any, constructor: any): boolean {
|
||||
if (value instanceof constructor) {
|
||||
return true;
|
||||
}
|
||||
if (typeof value === 'object' && value !== null) {
|
||||
const className = constructor.prototype[Symbol.toStringTag];
|
||||
const valueClassName =
|
||||
// We still need to support constructor's name to detect conflicts with older versions of this library.
|
||||
Symbol.toStringTag in value
|
||||
? value[Symbol.toStringTag]
|
||||
: value.constructor?.name;
|
||||
if (className === valueClassName) {
|
||||
const stringifiedValue = inspect(value);
|
||||
throw new Error(
|
||||
`Cannot use ${className} "${stringifiedValue}" from another module or realm.
|
||||
|
||||
Ensure that there is only one instance of "graphql" in the node_modules
|
||||
directory. If different versions of "graphql" are the dependencies of other
|
||||
relied on modules, use "resolutions" to ensure only one version is installed.
|
||||
|
||||
https://yarnpkg.com/en/docs/selective-version-resolutions
|
||||
|
||||
Duplicate "graphql" modules cannot be used at the same time since different
|
||||
versions may have different capabilities and behavior. The data from one
|
||||
version used in the function from another could produce confusing and
|
||||
spurious results.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
||||
|
||||
import inspect from "./inspect.mjs";
|
||||
/**
|
||||
* A replacement for instanceof which includes an error warning when multi-realm
|
||||
* constructors are detected.
|
||||
*/
|
||||
|
||||
// See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production
|
||||
// See: https://webpack.js.org/guides/production/
|
||||
export default process.env.NODE_ENV === 'production' ? // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
|
||||
// eslint-disable-next-line no-shadow
|
||||
function instanceOf(value, constructor) {
|
||||
return value instanceof constructor;
|
||||
} : // eslint-disable-next-line no-shadow
|
||||
function instanceOf(value, constructor) {
|
||||
if (value instanceof constructor) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_typeof(value) === 'object' && value !== null) {
|
||||
var _value$constructor;
|
||||
|
||||
var className = constructor.prototype[Symbol.toStringTag];
|
||||
var valueClassName = // We still need to support constructor's name to detect conflicts with older versions of this library.
|
||||
Symbol.toStringTag in value ? value[Symbol.toStringTag] : (_value$constructor = value.constructor) === null || _value$constructor === void 0 ? void 0 : _value$constructor.name;
|
||||
|
||||
if (className === valueClassName) {
|
||||
var stringifiedValue = inspect(value);
|
||||
throw new Error("Cannot use ".concat(className, " \"").concat(stringifiedValue, "\" from another module or realm.\n\nEnsure that there is only one instance of \"graphql\" in the node_modules\ndirectory. If different versions of \"graphql\" are the dependencies of other\nrelied on modules, use \"resolutions\" to ensure only one version is installed.\n\nhttps://yarnpkg.com/en/docs/selective-version-resolutions\n\nDuplicate \"graphql\" modules cannot be used at the same time since different\nversions may have different capabilities and behavior. The data from one\nversion used in the function from another could produce confusing and\nspurious results."));
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = invariant;
|
||||
|
||||
function invariant(condition, message) {
|
||||
var booleanCondition = Boolean(condition); // istanbul ignore else (See transformation done in './resources/inlineInvariant.js')
|
||||
|
||||
if (!booleanCondition) {
|
||||
throw new Error(message != null ? message : 'Unexpected invariant triggered.');
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// @flow strict
|
||||
export default function invariant(condition: mixed, message?: string): void {
|
||||
const booleanCondition = Boolean(condition);
|
||||
// istanbul ignore else (See transformation done in './resources/inlineInvariant.js')
|
||||
if (!booleanCondition) {
|
||||
throw new Error(
|
||||
message != null ? message : 'Unexpected invariant triggered.',
|
||||
);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
export default function invariant(condition, message) {
|
||||
var booleanCondition = Boolean(condition); // istanbul ignore else (See transformation done in './resources/inlineInvariant.js')
|
||||
|
||||
if (!booleanCondition) {
|
||||
throw new Error(message != null ? message : 'Unexpected invariant triggered.');
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = isAsyncIterable;
|
||||
|
||||
var _symbols = require("../polyfills/symbols.js");
|
||||
|
||||
// eslint-disable-next-line no-redeclare
|
||||
function isAsyncIterable(maybeAsyncIterable) {
|
||||
return typeof (maybeAsyncIterable === null || maybeAsyncIterable === void 0 ? void 0 : maybeAsyncIterable[_symbols.SYMBOL_ASYNC_ITERATOR]) === 'function';
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// @flow strict
|
||||
import { SYMBOL_ASYNC_ITERATOR } from '../polyfills/symbols';
|
||||
|
||||
/**
|
||||
* Returns true if the provided object implements the AsyncIterator protocol via
|
||||
* either implementing a `Symbol.asyncIterator` or `"@@asyncIterator"` method.
|
||||
*/
|
||||
declare function isAsyncIterable(value: mixed): boolean %checks(value instanceof
|
||||
AsyncIterable);
|
||||
|
||||
// eslint-disable-next-line no-redeclare
|
||||
export default function isAsyncIterable(maybeAsyncIterable) {
|
||||
return typeof maybeAsyncIterable?.[SYMBOL_ASYNC_ITERATOR] === 'function';
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import { SYMBOL_ASYNC_ITERATOR } from "../polyfills/symbols.mjs";
|
||||
/**
|
||||
* Returns true if the provided object implements the AsyncIterator protocol via
|
||||
* either implementing a `Symbol.asyncIterator` or `"@@asyncIterator"` method.
|
||||
*/
|
||||
|
||||
// eslint-disable-next-line no-redeclare
|
||||
export default function isAsyncIterable(maybeAsyncIterable) {
|
||||
return typeof (maybeAsyncIterable === null || maybeAsyncIterable === void 0 ? void 0 : maybeAsyncIterable[SYMBOL_ASYNC_ITERATOR]) === 'function';
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = isObjectLike;
|
||||
|
||||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
||||
|
||||
/**
|
||||
* Return true if `value` is object-like. A value is object-like if it's not
|
||||
* `null` and has a `typeof` result of "object".
|
||||
*/
|
||||
function isObjectLike(value) {
|
||||
return _typeof(value) == 'object' && value !== null;
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// @flow strict
|
||||
/**
|
||||
* Return true if `value` is object-like. A value is object-like if it's not
|
||||
* `null` and has a `typeof` result of "object".
|
||||
*/
|
||||
export default function isObjectLike(value: mixed): boolean %checks {
|
||||
return typeof value == 'object' && value !== null;
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
||||
|
||||
/**
|
||||
* Return true if `value` is object-like. A value is object-like if it's not
|
||||
* `null` and has a `typeof` result of "object".
|
||||
*/
|
||||
export default function isObjectLike(value) {
|
||||
return _typeof(value) == 'object' && value !== null;
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = isPromise;
|
||||
|
||||
/**
|
||||
* Returns true if the value acts like a Promise, i.e. has a "then" function,
|
||||
* otherwise returns false.
|
||||
*/
|
||||
// eslint-disable-next-line no-redeclare
|
||||
function isPromise(value) {
|
||||
return typeof (value === null || value === void 0 ? void 0 : value.then) === 'function';
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// @flow strict
|
||||
/**
|
||||
* Returns true if the value acts like a Promise, i.e. has a "then" function,
|
||||
* otherwise returns false.
|
||||
*/
|
||||
declare function isPromise(value: mixed): boolean %checks(value instanceof
|
||||
Promise);
|
||||
|
||||
// eslint-disable-next-line no-redeclare
|
||||
export default function isPromise(value) {
|
||||
return typeof value?.then === 'function';
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Returns true if the value acts like a Promise, i.e. has a "then" function,
|
||||
* otherwise returns false.
|
||||
*/
|
||||
// eslint-disable-next-line no-redeclare
|
||||
export default function isPromise(value) {
|
||||
return typeof (value === null || value === void 0 ? void 0 : value.then) === 'function';
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = keyMap;
|
||||
|
||||
/**
|
||||
* Creates a keyed JS object from an array, given a function to produce the keys
|
||||
* for each value in the array.
|
||||
*
|
||||
* This provides a convenient lookup for the array items if the key function
|
||||
* produces unique results.
|
||||
*
|
||||
* const phoneBook = [
|
||||
* { name: 'Jon', num: '555-1234' },
|
||||
* { name: 'Jenny', num: '867-5309' }
|
||||
* ]
|
||||
*
|
||||
* // { Jon: { name: 'Jon', num: '555-1234' },
|
||||
* // Jenny: { name: 'Jenny', num: '867-5309' } }
|
||||
* const entriesByName = keyMap(
|
||||
* phoneBook,
|
||||
* entry => entry.name
|
||||
* )
|
||||
*
|
||||
* // { name: 'Jenny', num: '857-6309' }
|
||||
* const jennyEntry = entriesByName['Jenny']
|
||||
*
|
||||
*/
|
||||
function keyMap(list, keyFn) {
|
||||
return list.reduce(function (map, item) {
|
||||
map[keyFn(item)] = item;
|
||||
return map;
|
||||
}, Object.create(null));
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
// @flow strict
|
||||
import type { ObjMap } from './ObjMap';
|
||||
|
||||
/**
|
||||
* Creates a keyed JS object from an array, given a function to produce the keys
|
||||
* for each value in the array.
|
||||
*
|
||||
* This provides a convenient lookup for the array items if the key function
|
||||
* produces unique results.
|
||||
*
|
||||
* const phoneBook = [
|
||||
* { name: 'Jon', num: '555-1234' },
|
||||
* { name: 'Jenny', num: '867-5309' }
|
||||
* ]
|
||||
*
|
||||
* // { Jon: { name: 'Jon', num: '555-1234' },
|
||||
* // Jenny: { name: 'Jenny', num: '867-5309' } }
|
||||
* const entriesByName = keyMap(
|
||||
* phoneBook,
|
||||
* entry => entry.name
|
||||
* )
|
||||
*
|
||||
* // { name: 'Jenny', num: '857-6309' }
|
||||
* const jennyEntry = entriesByName['Jenny']
|
||||
*
|
||||
*/
|
||||
export default function keyMap<T>(
|
||||
list: $ReadOnlyArray<T>,
|
||||
keyFn: (item: T) => string,
|
||||
): ObjMap<T> {
|
||||
return list.reduce((map, item) => {
|
||||
map[keyFn(item)] = item;
|
||||
return map;
|
||||
}, Object.create(null));
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Creates a keyed JS object from an array, given a function to produce the keys
|
||||
* for each value in the array.
|
||||
*
|
||||
* This provides a convenient lookup for the array items if the key function
|
||||
* produces unique results.
|
||||
*
|
||||
* const phoneBook = [
|
||||
* { name: 'Jon', num: '555-1234' },
|
||||
* { name: 'Jenny', num: '867-5309' }
|
||||
* ]
|
||||
*
|
||||
* // { Jon: { name: 'Jon', num: '555-1234' },
|
||||
* // Jenny: { name: 'Jenny', num: '867-5309' } }
|
||||
* const entriesByName = keyMap(
|
||||
* phoneBook,
|
||||
* entry => entry.name
|
||||
* )
|
||||
*
|
||||
* // { name: 'Jenny', num: '857-6309' }
|
||||
* const jennyEntry = entriesByName['Jenny']
|
||||
*
|
||||
*/
|
||||
export default function keyMap(list, keyFn) {
|
||||
return list.reduce(function (map, item) {
|
||||
map[keyFn(item)] = item;
|
||||
return map;
|
||||
}, Object.create(null));
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = keyValMap;
|
||||
|
||||
/**
|
||||
* Creates a keyed JS object from an array, given a function to produce the keys
|
||||
* and a function to produce the values from each item in the array.
|
||||
*
|
||||
* const phoneBook = [
|
||||
* { name: 'Jon', num: '555-1234' },
|
||||
* { name: 'Jenny', num: '867-5309' }
|
||||
* ]
|
||||
*
|
||||
* // { Jon: '555-1234', Jenny: '867-5309' }
|
||||
* const phonesByName = keyValMap(
|
||||
* phoneBook,
|
||||
* entry => entry.name,
|
||||
* entry => entry.num
|
||||
* )
|
||||
*
|
||||
*/
|
||||
function keyValMap(list, keyFn, valFn) {
|
||||
return list.reduce(function (map, item) {
|
||||
map[keyFn(item)] = valFn(item);
|
||||
return map;
|
||||
}, Object.create(null));
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
// @flow strict
|
||||
import type { ObjMap } from './ObjMap';
|
||||
|
||||
/**
|
||||
* Creates a keyed JS object from an array, given a function to produce the keys
|
||||
* and a function to produce the values from each item in the array.
|
||||
*
|
||||
* const phoneBook = [
|
||||
* { name: 'Jon', num: '555-1234' },
|
||||
* { name: 'Jenny', num: '867-5309' }
|
||||
* ]
|
||||
*
|
||||
* // { Jon: '555-1234', Jenny: '867-5309' }
|
||||
* const phonesByName = keyValMap(
|
||||
* phoneBook,
|
||||
* entry => entry.name,
|
||||
* entry => entry.num
|
||||
* )
|
||||
*
|
||||
*/
|
||||
export default function keyValMap<T, V>(
|
||||
list: $ReadOnlyArray<T>,
|
||||
keyFn: (item: T) => string,
|
||||
valFn: (item: T) => V,
|
||||
): ObjMap<V> {
|
||||
return list.reduce((map, item) => {
|
||||
map[keyFn(item)] = valFn(item);
|
||||
return map;
|
||||
}, Object.create(null));
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Creates a keyed JS object from an array, given a function to produce the keys
|
||||
* and a function to produce the values from each item in the array.
|
||||
*
|
||||
* const phoneBook = [
|
||||
* { name: 'Jon', num: '555-1234' },
|
||||
* { name: 'Jenny', num: '867-5309' }
|
||||
* ]
|
||||
*
|
||||
* // { Jon: '555-1234', Jenny: '867-5309' }
|
||||
* const phonesByName = keyValMap(
|
||||
* phoneBook,
|
||||
* entry => entry.name,
|
||||
* entry => entry.num
|
||||
* )
|
||||
*
|
||||
*/
|
||||
export default function keyValMap(list, keyFn, valFn) {
|
||||
return list.reduce(function (map, item) {
|
||||
map[keyFn(item)] = valFn(item);
|
||||
return map;
|
||||
}, Object.create(null));
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = mapValue;
|
||||
|
||||
var _objectEntries3 = _interopRequireDefault(require("../polyfills/objectEntries.js"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
/**
|
||||
* Creates an object map with the same keys as `map` and values generated by
|
||||
* running each value of `map` thru `fn`.
|
||||
*/
|
||||
function mapValue(map, fn) {
|
||||
var result = Object.create(null);
|
||||
|
||||
for (var _i2 = 0, _objectEntries2 = (0, _objectEntries3.default)(map); _i2 < _objectEntries2.length; _i2++) {
|
||||
var _ref2 = _objectEntries2[_i2];
|
||||
var _key = _ref2[0];
|
||||
var _value = _ref2[1];
|
||||
result[_key] = fn(_value, _key);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// @flow strict
|
||||
import objectEntries from '../polyfills/objectEntries';
|
||||
|
||||
import type { ObjMap } from './ObjMap';
|
||||
|
||||
/**
|
||||
* Creates an object map with the same keys as `map` and values generated by
|
||||
* running each value of `map` thru `fn`.
|
||||
*/
|
||||
export default function mapValue<T, V>(
|
||||
map: ObjMap<T>,
|
||||
fn: (value: T, key: string) => V,
|
||||
): ObjMap<V> {
|
||||
const result = Object.create(null);
|
||||
|
||||
for (const [key, value] of objectEntries(map)) {
|
||||
result[key] = fn(value, key);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
import objectEntries from "../polyfills/objectEntries.mjs";
|
||||
|
||||
/**
|
||||
* Creates an object map with the same keys as `map` and values generated by
|
||||
* running each value of `map` thru `fn`.
|
||||
*/
|
||||
export default function mapValue(map, fn) {
|
||||
var result = Object.create(null);
|
||||
|
||||
for (var _i2 = 0, _objectEntries2 = objectEntries(map); _i2 < _objectEntries2.length; _i2++) {
|
||||
var _ref2 = _objectEntries2[_i2];
|
||||
var _key = _ref2[0];
|
||||
var _value = _ref2[1];
|
||||
result[_key] = fn(_value, _key);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = memoize3;
|
||||
|
||||
/**
|
||||
* Memoizes the provided three-argument function.
|
||||
*/
|
||||
function memoize3(fn) {
|
||||
var cache0;
|
||||
return function memoized(a1, a2, a3) {
|
||||
if (!cache0) {
|
||||
cache0 = new WeakMap();
|
||||
}
|
||||
|
||||
var cache1 = cache0.get(a1);
|
||||
var cache2;
|
||||
|
||||
if (cache1) {
|
||||
cache2 = cache1.get(a2);
|
||||
|
||||
if (cache2) {
|
||||
var cachedValue = cache2.get(a3);
|
||||
|
||||
if (cachedValue !== undefined) {
|
||||
return cachedValue;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cache1 = new WeakMap();
|
||||
cache0.set(a1, cache1);
|
||||
}
|
||||
|
||||
if (!cache2) {
|
||||
cache2 = new WeakMap();
|
||||
cache1.set(a2, cache2);
|
||||
}
|
||||
|
||||
var newValue = fn(a1, a2, a3);
|
||||
cache2.set(a3, newValue);
|
||||
return newValue;
|
||||
};
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
// @flow strict
|
||||
/**
|
||||
* Memoizes the provided three-argument function.
|
||||
*/
|
||||
export default function memoize3<
|
||||
A1: { ... } | $ReadOnlyArray<mixed>,
|
||||
A2: { ... } | $ReadOnlyArray<mixed>,
|
||||
A3: { ... } | $ReadOnlyArray<mixed>,
|
||||
R: mixed,
|
||||
>(fn: (A1, A2, A3) => R): (A1, A2, A3) => R {
|
||||
let cache0;
|
||||
|
||||
return function memoized(a1, a2, a3) {
|
||||
if (!cache0) {
|
||||
cache0 = new WeakMap();
|
||||
}
|
||||
let cache1 = cache0.get(a1);
|
||||
let cache2;
|
||||
if (cache1) {
|
||||
cache2 = cache1.get(a2);
|
||||
if (cache2) {
|
||||
const cachedValue = cache2.get(a3);
|
||||
if (cachedValue !== undefined) {
|
||||
return cachedValue;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cache1 = new WeakMap();
|
||||
cache0.set(a1, cache1);
|
||||
}
|
||||
if (!cache2) {
|
||||
cache2 = new WeakMap();
|
||||
cache1.set(a2, cache2);
|
||||
}
|
||||
const newValue = fn(a1, a2, a3);
|
||||
cache2.set(a3, newValue);
|
||||
return newValue;
|
||||
};
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Memoizes the provided three-argument function.
|
||||
*/
|
||||
export default function memoize3(fn) {
|
||||
var cache0;
|
||||
return function memoized(a1, a2, a3) {
|
||||
if (!cache0) {
|
||||
cache0 = new WeakMap();
|
||||
}
|
||||
|
||||
var cache1 = cache0.get(a1);
|
||||
var cache2;
|
||||
|
||||
if (cache1) {
|
||||
cache2 = cache1.get(a2);
|
||||
|
||||
if (cache2) {
|
||||
var cachedValue = cache2.get(a3);
|
||||
|
||||
if (cachedValue !== undefined) {
|
||||
return cachedValue;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cache1 = new WeakMap();
|
||||
cache0.set(a1, cache1);
|
||||
}
|
||||
|
||||
if (!cache2) {
|
||||
cache2 = new WeakMap();
|
||||
cache1.set(a2, cache2);
|
||||
}
|
||||
|
||||
var newValue = fn(a1, a2, a3);
|
||||
cache2.set(a3, newValue);
|
||||
return newValue;
|
||||
};
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = naturalCompare;
|
||||
|
||||
/**
|
||||
* Returns a number indicating whether a reference string comes before, or after,
|
||||
* or is the same as the given string in natural sort order.
|
||||
*
|
||||
* See: https://en.wikipedia.org/wiki/Natural_sort_order
|
||||
*
|
||||
*/
|
||||
function naturalCompare(aStr, bStr) {
|
||||
var aIdx = 0;
|
||||
var bIdx = 0;
|
||||
|
||||
while (aIdx < aStr.length && bIdx < bStr.length) {
|
||||
var aChar = aStr.charCodeAt(aIdx);
|
||||
var bChar = bStr.charCodeAt(bIdx);
|
||||
|
||||
if (isDigit(aChar) && isDigit(bChar)) {
|
||||
var aNum = 0;
|
||||
|
||||
do {
|
||||
++aIdx;
|
||||
aNum = aNum * 10 + aChar - DIGIT_0;
|
||||
aChar = aStr.charCodeAt(aIdx);
|
||||
} while (isDigit(aChar) && aNum > 0);
|
||||
|
||||
var bNum = 0;
|
||||
|
||||
do {
|
||||
++bIdx;
|
||||
bNum = bNum * 10 + bChar - DIGIT_0;
|
||||
bChar = bStr.charCodeAt(bIdx);
|
||||
} while (isDigit(bChar) && bNum > 0);
|
||||
|
||||
if (aNum < bNum) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (aNum > bNum) {
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
if (aChar < bChar) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (aChar > bChar) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
++aIdx;
|
||||
++bIdx;
|
||||
}
|
||||
}
|
||||
|
||||
return aStr.length - bStr.length;
|
||||
}
|
||||
|
||||
var DIGIT_0 = 48;
|
||||
var DIGIT_9 = 57;
|
||||
|
||||
function isDigit(code) {
|
||||
return !isNaN(code) && DIGIT_0 <= code && code <= DIGIT_9;
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
// @flow strict
|
||||
/**
|
||||
* Returns a number indicating whether a reference string comes before, or after,
|
||||
* or is the same as the given string in natural sort order.
|
||||
*
|
||||
* See: https://en.wikipedia.org/wiki/Natural_sort_order
|
||||
*
|
||||
*/
|
||||
export default function naturalCompare(aStr: string, bStr: string): number {
|
||||
let aIdx = 0;
|
||||
let bIdx = 0;
|
||||
|
||||
while (aIdx < aStr.length && bIdx < bStr.length) {
|
||||
let aChar = aStr.charCodeAt(aIdx);
|
||||
let bChar = bStr.charCodeAt(bIdx);
|
||||
|
||||
if (isDigit(aChar) && isDigit(bChar)) {
|
||||
let aNum = 0;
|
||||
do {
|
||||
++aIdx;
|
||||
aNum = aNum * 10 + aChar - DIGIT_0;
|
||||
aChar = aStr.charCodeAt(aIdx);
|
||||
} while (isDigit(aChar) && aNum > 0);
|
||||
|
||||
let bNum = 0;
|
||||
do {
|
||||
++bIdx;
|
||||
bNum = bNum * 10 + bChar - DIGIT_0;
|
||||
bChar = bStr.charCodeAt(bIdx);
|
||||
} while (isDigit(bChar) && bNum > 0);
|
||||
|
||||
if (aNum < bNum) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (aNum > bNum) {
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
if (aChar < bChar) {
|
||||
return -1;
|
||||
}
|
||||
if (aChar > bChar) {
|
||||
return 1;
|
||||
}
|
||||
++aIdx;
|
||||
++bIdx;
|
||||
}
|
||||
}
|
||||
|
||||
return aStr.length - bStr.length;
|
||||
}
|
||||
|
||||
const DIGIT_0 = 48;
|
||||
const DIGIT_9 = 57;
|
||||
|
||||
function isDigit(code: number): boolean {
|
||||
return !isNaN(code) && DIGIT_0 <= code && code <= DIGIT_9;
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Returns a number indicating whether a reference string comes before, or after,
|
||||
* or is the same as the given string in natural sort order.
|
||||
*
|
||||
* See: https://en.wikipedia.org/wiki/Natural_sort_order
|
||||
*
|
||||
*/
|
||||
export default function naturalCompare(aStr, bStr) {
|
||||
var aIdx = 0;
|
||||
var bIdx = 0;
|
||||
|
||||
while (aIdx < aStr.length && bIdx < bStr.length) {
|
||||
var aChar = aStr.charCodeAt(aIdx);
|
||||
var bChar = bStr.charCodeAt(bIdx);
|
||||
|
||||
if (isDigit(aChar) && isDigit(bChar)) {
|
||||
var aNum = 0;
|
||||
|
||||
do {
|
||||
++aIdx;
|
||||
aNum = aNum * 10 + aChar - DIGIT_0;
|
||||
aChar = aStr.charCodeAt(aIdx);
|
||||
} while (isDigit(aChar) && aNum > 0);
|
||||
|
||||
var bNum = 0;
|
||||
|
||||
do {
|
||||
++bIdx;
|
||||
bNum = bNum * 10 + bChar - DIGIT_0;
|
||||
bChar = bStr.charCodeAt(bIdx);
|
||||
} while (isDigit(bChar) && bNum > 0);
|
||||
|
||||
if (aNum < bNum) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (aNum > bNum) {
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
if (aChar < bChar) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (aChar > bChar) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
++aIdx;
|
||||
++bIdx;
|
||||
}
|
||||
}
|
||||
|
||||
return aStr.length - bStr.length;
|
||||
}
|
||||
var DIGIT_0 = 48;
|
||||
var DIGIT_9 = 57;
|
||||
|
||||
function isDigit(code) {
|
||||
return !isNaN(code) && DIGIT_0 <= code && code <= DIGIT_9;
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
|
||||
var nodejsCustomInspectSymbol = typeof Symbol === 'function' && typeof Symbol.for === 'function' ? Symbol.for('nodejs.util.inspect.custom') : undefined;
|
||||
var _default = nodejsCustomInspectSymbol;
|
||||
exports.default = _default;
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// @flow strict
|
||||
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
|
||||
const nodejsCustomInspectSymbol =
|
||||
typeof Symbol === 'function' && typeof Symbol.for === 'function'
|
||||
? Symbol.for('nodejs.util.inspect.custom')
|
||||
: undefined;
|
||||
|
||||
export default nodejsCustomInspectSymbol;
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
|
||||
var nodejsCustomInspectSymbol = typeof Symbol === 'function' && typeof Symbol.for === 'function' ? Symbol.for('nodejs.util.inspect.custom') : undefined;
|
||||
export default nodejsCustomInspectSymbol;
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = printPathArray;
|
||||
|
||||
/**
|
||||
* Build a string describing the path.
|
||||
*/
|
||||
function printPathArray(path) {
|
||||
return path.map(function (key) {
|
||||
return typeof key === 'number' ? '[' + key.toString() + ']' : '.' + key;
|
||||
}).join('');
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// @flow strict
|
||||
/**
|
||||
* Build a string describing the path.
|
||||
*/
|
||||
export default function printPathArray(
|
||||
path: $ReadOnlyArray<string | number>,
|
||||
): string {
|
||||
return path
|
||||
.map((key) =>
|
||||
typeof key === 'number' ? '[' + key.toString() + ']' : '.' + key,
|
||||
)
|
||||
.join('');
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Build a string describing the path.
|
||||
*/
|
||||
export default function printPathArray(path) {
|
||||
return path.map(function (key) {
|
||||
return typeof key === 'number' ? '[' + key.toString() + ']' : '.' + key;
|
||||
}).join('');
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = promiseForObject;
|
||||
|
||||
/**
|
||||
* This function transforms a JS object `ObjMap<Promise<T>>` into
|
||||
* a `Promise<ObjMap<T>>`
|
||||
*
|
||||
* This is akin to bluebird's `Promise.props`, but implemented only using
|
||||
* `Promise.all` so it will work with any implementation of ES6 promises.
|
||||
*/
|
||||
function promiseForObject(object) {
|
||||
var keys = Object.keys(object);
|
||||
var valuesAndPromises = keys.map(function (name) {
|
||||
return object[name];
|
||||
});
|
||||
return Promise.all(valuesAndPromises).then(function (values) {
|
||||
return values.reduce(function (resolvedObject, value, i) {
|
||||
resolvedObject[keys[i]] = value;
|
||||
return resolvedObject;
|
||||
}, Object.create(null));
|
||||
});
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
// @flow strict
|
||||
import type { ObjMap } from './ObjMap';
|
||||
|
||||
/**
|
||||
* This function transforms a JS object `ObjMap<Promise<T>>` into
|
||||
* a `Promise<ObjMap<T>>`
|
||||
*
|
||||
* This is akin to bluebird's `Promise.props`, but implemented only using
|
||||
* `Promise.all` so it will work with any implementation of ES6 promises.
|
||||
*/
|
||||
export default function promiseForObject<T>(
|
||||
object: ObjMap<Promise<T>>,
|
||||
): Promise<ObjMap<T>> {
|
||||
const keys = Object.keys(object);
|
||||
const valuesAndPromises = keys.map((name) => object[name]);
|
||||
return Promise.all(valuesAndPromises).then((values) =>
|
||||
values.reduce((resolvedObject, value, i) => {
|
||||
resolvedObject[keys[i]] = value;
|
||||
return resolvedObject;
|
||||
}, Object.create(null)),
|
||||
);
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* This function transforms a JS object `ObjMap<Promise<T>>` into
|
||||
* a `Promise<ObjMap<T>>`
|
||||
*
|
||||
* This is akin to bluebird's `Promise.props`, but implemented only using
|
||||
* `Promise.all` so it will work with any implementation of ES6 promises.
|
||||
*/
|
||||
export default function promiseForObject(object) {
|
||||
var keys = Object.keys(object);
|
||||
var valuesAndPromises = keys.map(function (name) {
|
||||
return object[name];
|
||||
});
|
||||
return Promise.all(valuesAndPromises).then(function (values) {
|
||||
return values.reduce(function (resolvedObject, value, i) {
|
||||
resolvedObject[keys[i]] = value;
|
||||
return resolvedObject;
|
||||
}, Object.create(null));
|
||||
});
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = promiseReduce;
|
||||
|
||||
var _isPromise = _interopRequireDefault(require("./isPromise.js"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
/**
|
||||
* Similar to Array.prototype.reduce(), however the reducing callback may return
|
||||
* a Promise, in which case reduction will continue after each promise resolves.
|
||||
*
|
||||
* If the callback does not return a Promise, then this function will also not
|
||||
* return a Promise.
|
||||
*/
|
||||
function promiseReduce(values, callback, initialValue) {
|
||||
return values.reduce(function (previous, value) {
|
||||
return (0, _isPromise.default)(previous) ? previous.then(function (resolved) {
|
||||
return callback(resolved, value);
|
||||
}) : callback(previous, value);
|
||||
}, initialValue);
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
// @flow strict
|
||||
import type { PromiseOrValue } from './PromiseOrValue';
|
||||
|
||||
import isPromise from './isPromise';
|
||||
|
||||
/**
|
||||
* Similar to Array.prototype.reduce(), however the reducing callback may return
|
||||
* a Promise, in which case reduction will continue after each promise resolves.
|
||||
*
|
||||
* If the callback does not return a Promise, then this function will also not
|
||||
* return a Promise.
|
||||
*/
|
||||
export default function promiseReduce<T, U>(
|
||||
values: $ReadOnlyArray<T>,
|
||||
callback: (U, T) => PromiseOrValue<U>,
|
||||
initialValue: PromiseOrValue<U>,
|
||||
): PromiseOrValue<U> {
|
||||
return values.reduce(
|
||||
(previous, value) =>
|
||||
isPromise(previous)
|
||||
? previous.then((resolved) => callback(resolved, value))
|
||||
: callback(previous, value),
|
||||
initialValue,
|
||||
);
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import isPromise from "./isPromise.mjs";
|
||||
/**
|
||||
* Similar to Array.prototype.reduce(), however the reducing callback may return
|
||||
* a Promise, in which case reduction will continue after each promise resolves.
|
||||
*
|
||||
* If the callback does not return a Promise, then this function will also not
|
||||
* return a Promise.
|
||||
*/
|
||||
|
||||
export default function promiseReduce(values, callback, initialValue) {
|
||||
return values.reduce(function (previous, value) {
|
||||
return isPromise(previous) ? previous.then(function (resolved) {
|
||||
return callback(resolved, value);
|
||||
}) : callback(previous, value);
|
||||
}, initialValue);
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = safeArrayFrom;
|
||||
|
||||
var _symbols = require("../polyfills/symbols.js");
|
||||
|
||||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
||||
|
||||
/**
|
||||
* Safer version of `Array.from` that return `null` if value isn't convertible to array.
|
||||
* Also protects against Array-like objects without items.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* safeArrayFrom([ 1, 2, 3 ]) // [1, 2, 3]
|
||||
* safeArrayFrom('ABC') // null
|
||||
* safeArrayFrom({ length: 1 }) // null
|
||||
* safeArrayFrom({ length: 1, 0: 'Alpha' }) // ['Alpha']
|
||||
* safeArrayFrom({ key: 'value' }) // null
|
||||
* safeArrayFrom(new Map()) // []
|
||||
*
|
||||
*/
|
||||
function safeArrayFrom(collection) {
|
||||
var mapFn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (item) {
|
||||
return item;
|
||||
};
|
||||
|
||||
if (collection == null || _typeof(collection) !== 'object') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Array.isArray(collection)) {
|
||||
return collection.map(mapFn);
|
||||
} // Is Iterable?
|
||||
|
||||
|
||||
var iteratorMethod = collection[_symbols.SYMBOL_ITERATOR];
|
||||
|
||||
if (typeof iteratorMethod === 'function') {
|
||||
// $FlowFixMe[incompatible-use]
|
||||
var iterator = iteratorMethod.call(collection);
|
||||
var result = [];
|
||||
var step;
|
||||
|
||||
for (var i = 0; !(step = iterator.next()).done; ++i) {
|
||||
result.push(mapFn(step.value, i));
|
||||
}
|
||||
|
||||
return result;
|
||||
} // Is Array like?
|
||||
|
||||
|
||||
var length = collection.length;
|
||||
|
||||
if (typeof length === 'number' && length >= 0 && length % 1 === 0) {
|
||||
var _result = [];
|
||||
|
||||
for (var _i = 0; _i < length; ++_i) {
|
||||
if (!Object.prototype.hasOwnProperty.call(collection, _i)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
_result.push(mapFn(collection[String(_i)], _i));
|
||||
}
|
||||
|
||||
return _result;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
// @flow strict
|
||||
import { SYMBOL_ITERATOR } from '../polyfills/symbols';
|
||||
|
||||
/**
|
||||
* Safer version of `Array.from` that return `null` if value isn't convertible to array.
|
||||
* Also protects against Array-like objects without items.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* safeArrayFrom([ 1, 2, 3 ]) // [1, 2, 3]
|
||||
* safeArrayFrom('ABC') // null
|
||||
* safeArrayFrom({ length: 1 }) // null
|
||||
* safeArrayFrom({ length: 1, 0: 'Alpha' }) // ['Alpha']
|
||||
* safeArrayFrom({ key: 'value' }) // null
|
||||
* safeArrayFrom(new Map()) // []
|
||||
*
|
||||
*/
|
||||
export default function safeArrayFrom<T>(
|
||||
collection: mixed,
|
||||
mapFn: (elem: mixed, index: number) => T = (item) => ((item: any): T),
|
||||
): Array<T> | null {
|
||||
if (collection == null || typeof collection !== 'object') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Array.isArray(collection)) {
|
||||
return collection.map(mapFn);
|
||||
}
|
||||
|
||||
// Is Iterable?
|
||||
const iteratorMethod = collection[SYMBOL_ITERATOR];
|
||||
if (typeof iteratorMethod === 'function') {
|
||||
// $FlowFixMe[incompatible-use]
|
||||
const iterator = iteratorMethod.call(collection);
|
||||
const result = [];
|
||||
let step;
|
||||
|
||||
for (let i = 0; !(step = iterator.next()).done; ++i) {
|
||||
result.push(mapFn(step.value, i));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Is Array like?
|
||||
const length = collection.length;
|
||||
if (typeof length === 'number' && length >= 0 && length % 1 === 0) {
|
||||
const result = [];
|
||||
for (let i = 0; i < length; ++i) {
|
||||
if (!Object.prototype.hasOwnProperty.call(collection, i)) {
|
||||
return null;
|
||||
}
|
||||
result.push(mapFn(collection[String(i)], i));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
||||
|
||||
import { SYMBOL_ITERATOR } from "../polyfills/symbols.mjs";
|
||||
/**
|
||||
* Safer version of `Array.from` that return `null` if value isn't convertible to array.
|
||||
* Also protects against Array-like objects without items.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* safeArrayFrom([ 1, 2, 3 ]) // [1, 2, 3]
|
||||
* safeArrayFrom('ABC') // null
|
||||
* safeArrayFrom({ length: 1 }) // null
|
||||
* safeArrayFrom({ length: 1, 0: 'Alpha' }) // ['Alpha']
|
||||
* safeArrayFrom({ key: 'value' }) // null
|
||||
* safeArrayFrom(new Map()) // []
|
||||
*
|
||||
*/
|
||||
|
||||
export default function safeArrayFrom(collection) {
|
||||
var mapFn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (item) {
|
||||
return item;
|
||||
};
|
||||
|
||||
if (collection == null || _typeof(collection) !== 'object') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Array.isArray(collection)) {
|
||||
return collection.map(mapFn);
|
||||
} // Is Iterable?
|
||||
|
||||
|
||||
var iteratorMethod = collection[SYMBOL_ITERATOR];
|
||||
|
||||
if (typeof iteratorMethod === 'function') {
|
||||
// $FlowFixMe[incompatible-use]
|
||||
var iterator = iteratorMethod.call(collection);
|
||||
var result = [];
|
||||
var step;
|
||||
|
||||
for (var i = 0; !(step = iterator.next()).done; ++i) {
|
||||
result.push(mapFn(step.value, i));
|
||||
}
|
||||
|
||||
return result;
|
||||
} // Is Array like?
|
||||
|
||||
|
||||
var length = collection.length;
|
||||
|
||||
if (typeof length === 'number' && length >= 0 && length % 1 === 0) {
|
||||
var _result = [];
|
||||
|
||||
for (var _i = 0; _i < length; ++_i) {
|
||||
if (!Object.prototype.hasOwnProperty.call(collection, _i)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
_result.push(mapFn(collection[String(_i)], _i));
|
||||
}
|
||||
|
||||
return _result;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = suggestionList;
|
||||
|
||||
var _naturalCompare = _interopRequireDefault(require("./naturalCompare.js"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
/**
|
||||
* Given an invalid input string and a list of valid options, returns a filtered
|
||||
* list of valid options sorted based on their similarity with the input.
|
||||
*/
|
||||
function suggestionList(input, options) {
|
||||
var optionsByDistance = Object.create(null);
|
||||
var lexicalDistance = new LexicalDistance(input);
|
||||
var threshold = Math.floor(input.length * 0.4) + 1;
|
||||
|
||||
for (var _i2 = 0; _i2 < options.length; _i2++) {
|
||||
var option = options[_i2];
|
||||
var distance = lexicalDistance.measure(option, threshold);
|
||||
|
||||
if (distance !== undefined) {
|
||||
optionsByDistance[option] = distance;
|
||||
}
|
||||
}
|
||||
|
||||
return Object.keys(optionsByDistance).sort(function (a, b) {
|
||||
var distanceDiff = optionsByDistance[a] - optionsByDistance[b];
|
||||
return distanceDiff !== 0 ? distanceDiff : (0, _naturalCompare.default)(a, b);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Computes the lexical distance between strings A and B.
|
||||
*
|
||||
* The "distance" between two strings is given by counting the minimum number
|
||||
* of edits needed to transform string A into string B. An edit can be an
|
||||
* insertion, deletion, or substitution of a single character, or a swap of two
|
||||
* adjacent characters.
|
||||
*
|
||||
* Includes a custom alteration from Damerau-Levenshtein to treat case changes
|
||||
* as a single edit which helps identify mis-cased values with an edit distance
|
||||
* of 1.
|
||||
*
|
||||
* This distance can be useful for detecting typos in input or sorting
|
||||
*/
|
||||
|
||||
|
||||
var LexicalDistance = /*#__PURE__*/function () {
|
||||
function LexicalDistance(input) {
|
||||
this._input = input;
|
||||
this._inputLowerCase = input.toLowerCase();
|
||||
this._inputArray = stringToArray(this._inputLowerCase);
|
||||
this._rows = [new Array(input.length + 1).fill(0), new Array(input.length + 1).fill(0), new Array(input.length + 1).fill(0)];
|
||||
}
|
||||
|
||||
var _proto = LexicalDistance.prototype;
|
||||
|
||||
_proto.measure = function measure(option, threshold) {
|
||||
if (this._input === option) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
var optionLowerCase = option.toLowerCase(); // Any case change counts as a single edit
|
||||
|
||||
if (this._inputLowerCase === optionLowerCase) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
var a = stringToArray(optionLowerCase);
|
||||
var b = this._inputArray;
|
||||
|
||||
if (a.length < b.length) {
|
||||
var tmp = a;
|
||||
a = b;
|
||||
b = tmp;
|
||||
}
|
||||
|
||||
var aLength = a.length;
|
||||
var bLength = b.length;
|
||||
|
||||
if (aLength - bLength > threshold) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
var rows = this._rows;
|
||||
|
||||
for (var j = 0; j <= bLength; j++) {
|
||||
rows[0][j] = j;
|
||||
}
|
||||
|
||||
for (var i = 1; i <= aLength; i++) {
|
||||
var upRow = rows[(i - 1) % 3];
|
||||
var currentRow = rows[i % 3];
|
||||
var smallestCell = currentRow[0] = i;
|
||||
|
||||
for (var _j = 1; _j <= bLength; _j++) {
|
||||
var cost = a[i - 1] === b[_j - 1] ? 0 : 1;
|
||||
var currentCell = Math.min(upRow[_j] + 1, // delete
|
||||
currentRow[_j - 1] + 1, // insert
|
||||
upRow[_j - 1] + cost // substitute
|
||||
);
|
||||
|
||||
if (i > 1 && _j > 1 && a[i - 1] === b[_j - 2] && a[i - 2] === b[_j - 1]) {
|
||||
// transposition
|
||||
var doubleDiagonalCell = rows[(i - 2) % 3][_j - 2];
|
||||
currentCell = Math.min(currentCell, doubleDiagonalCell + 1);
|
||||
}
|
||||
|
||||
if (currentCell < smallestCell) {
|
||||
smallestCell = currentCell;
|
||||
}
|
||||
|
||||
currentRow[_j] = currentCell;
|
||||
} // Early exit, since distance can't go smaller than smallest element of the previous row.
|
||||
|
||||
|
||||
if (smallestCell > threshold) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
var distance = rows[aLength % 3][bLength];
|
||||
return distance <= threshold ? distance : undefined;
|
||||
};
|
||||
|
||||
return LexicalDistance;
|
||||
}();
|
||||
|
||||
function stringToArray(str) {
|
||||
var strLength = str.length;
|
||||
var array = new Array(strLength);
|
||||
|
||||
for (var i = 0; i < strLength; ++i) {
|
||||
array[i] = str.charCodeAt(i);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
// @flow strict
|
||||
import naturalCompare from './naturalCompare';
|
||||
|
||||
/**
|
||||
* Given an invalid input string and a list of valid options, returns a filtered
|
||||
* list of valid options sorted based on their similarity with the input.
|
||||
*/
|
||||
export default function suggestionList(
|
||||
input: string,
|
||||
options: $ReadOnlyArray<string>,
|
||||
): Array<string> {
|
||||
const optionsByDistance = Object.create(null);
|
||||
const lexicalDistance = new LexicalDistance(input);
|
||||
|
||||
const threshold = Math.floor(input.length * 0.4) + 1;
|
||||
for (const option of options) {
|
||||
const distance = lexicalDistance.measure(option, threshold);
|
||||
if (distance !== undefined) {
|
||||
optionsByDistance[option] = distance;
|
||||
}
|
||||
}
|
||||
|
||||
return Object.keys(optionsByDistance).sort((a, b) => {
|
||||
const distanceDiff = optionsByDistance[a] - optionsByDistance[b];
|
||||
return distanceDiff !== 0 ? distanceDiff : naturalCompare(a, b);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the lexical distance between strings A and B.
|
||||
*
|
||||
* The "distance" between two strings is given by counting the minimum number
|
||||
* of edits needed to transform string A into string B. An edit can be an
|
||||
* insertion, deletion, or substitution of a single character, or a swap of two
|
||||
* adjacent characters.
|
||||
*
|
||||
* Includes a custom alteration from Damerau-Levenshtein to treat case changes
|
||||
* as a single edit which helps identify mis-cased values with an edit distance
|
||||
* of 1.
|
||||
*
|
||||
* This distance can be useful for detecting typos in input or sorting
|
||||
*/
|
||||
class LexicalDistance {
|
||||
_input: string;
|
||||
_inputLowerCase: string;
|
||||
_inputArray: Array<number>;
|
||||
_rows: [Array<number>, Array<number>, Array<number>];
|
||||
|
||||
constructor(input: string) {
|
||||
this._input = input;
|
||||
this._inputLowerCase = input.toLowerCase();
|
||||
this._inputArray = stringToArray(this._inputLowerCase);
|
||||
|
||||
this._rows = [
|
||||
new Array(input.length + 1).fill(0),
|
||||
new Array(input.length + 1).fill(0),
|
||||
new Array(input.length + 1).fill(0),
|
||||
];
|
||||
}
|
||||
|
||||
measure(option: string, threshold: number): number | void {
|
||||
if (this._input === option) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const optionLowerCase = option.toLowerCase();
|
||||
|
||||
// Any case change counts as a single edit
|
||||
if (this._inputLowerCase === optionLowerCase) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
let a = stringToArray(optionLowerCase);
|
||||
let b = this._inputArray;
|
||||
|
||||
if (a.length < b.length) {
|
||||
const tmp = a;
|
||||
a = b;
|
||||
b = tmp;
|
||||
}
|
||||
const aLength = a.length;
|
||||
const bLength = b.length;
|
||||
|
||||
if (aLength - bLength > threshold) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const rows = this._rows;
|
||||
for (let j = 0; j <= bLength; j++) {
|
||||
rows[0][j] = j;
|
||||
}
|
||||
|
||||
for (let i = 1; i <= aLength; i++) {
|
||||
const upRow = rows[(i - 1) % 3];
|
||||
const currentRow = rows[i % 3];
|
||||
|
||||
let smallestCell = (currentRow[0] = i);
|
||||
for (let j = 1; j <= bLength; j++) {
|
||||
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
||||
|
||||
let currentCell = Math.min(
|
||||
upRow[j] + 1, // delete
|
||||
currentRow[j - 1] + 1, // insert
|
||||
upRow[j - 1] + cost, // substitute
|
||||
);
|
||||
|
||||
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
||||
// transposition
|
||||
const doubleDiagonalCell = rows[(i - 2) % 3][j - 2];
|
||||
currentCell = Math.min(currentCell, doubleDiagonalCell + 1);
|
||||
}
|
||||
|
||||
if (currentCell < smallestCell) {
|
||||
smallestCell = currentCell;
|
||||
}
|
||||
|
||||
currentRow[j] = currentCell;
|
||||
}
|
||||
|
||||
// Early exit, since distance can't go smaller than smallest element of the previous row.
|
||||
if (smallestCell > threshold) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
const distance = rows[aLength % 3][bLength];
|
||||
return distance <= threshold ? distance : undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function stringToArray(str: string): Array<number> {
|
||||
const strLength = str.length;
|
||||
const array = new Array(strLength);
|
||||
for (let i = 0; i < strLength; ++i) {
|
||||
array[i] = str.charCodeAt(i);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
import naturalCompare from "./naturalCompare.mjs";
|
||||
/**
|
||||
* Given an invalid input string and a list of valid options, returns a filtered
|
||||
* list of valid options sorted based on their similarity with the input.
|
||||
*/
|
||||
|
||||
export default function suggestionList(input, options) {
|
||||
var optionsByDistance = Object.create(null);
|
||||
var lexicalDistance = new LexicalDistance(input);
|
||||
var threshold = Math.floor(input.length * 0.4) + 1;
|
||||
|
||||
for (var _i2 = 0; _i2 < options.length; _i2++) {
|
||||
var option = options[_i2];
|
||||
var distance = lexicalDistance.measure(option, threshold);
|
||||
|
||||
if (distance !== undefined) {
|
||||
optionsByDistance[option] = distance;
|
||||
}
|
||||
}
|
||||
|
||||
return Object.keys(optionsByDistance).sort(function (a, b) {
|
||||
var distanceDiff = optionsByDistance[a] - optionsByDistance[b];
|
||||
return distanceDiff !== 0 ? distanceDiff : naturalCompare(a, b);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Computes the lexical distance between strings A and B.
|
||||
*
|
||||
* The "distance" between two strings is given by counting the minimum number
|
||||
* of edits needed to transform string A into string B. An edit can be an
|
||||
* insertion, deletion, or substitution of a single character, or a swap of two
|
||||
* adjacent characters.
|
||||
*
|
||||
* Includes a custom alteration from Damerau-Levenshtein to treat case changes
|
||||
* as a single edit which helps identify mis-cased values with an edit distance
|
||||
* of 1.
|
||||
*
|
||||
* This distance can be useful for detecting typos in input or sorting
|
||||
*/
|
||||
|
||||
var LexicalDistance = /*#__PURE__*/function () {
|
||||
function LexicalDistance(input) {
|
||||
this._input = input;
|
||||
this._inputLowerCase = input.toLowerCase();
|
||||
this._inputArray = stringToArray(this._inputLowerCase);
|
||||
this._rows = [new Array(input.length + 1).fill(0), new Array(input.length + 1).fill(0), new Array(input.length + 1).fill(0)];
|
||||
}
|
||||
|
||||
var _proto = LexicalDistance.prototype;
|
||||
|
||||
_proto.measure = function measure(option, threshold) {
|
||||
if (this._input === option) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
var optionLowerCase = option.toLowerCase(); // Any case change counts as a single edit
|
||||
|
||||
if (this._inputLowerCase === optionLowerCase) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
var a = stringToArray(optionLowerCase);
|
||||
var b = this._inputArray;
|
||||
|
||||
if (a.length < b.length) {
|
||||
var tmp = a;
|
||||
a = b;
|
||||
b = tmp;
|
||||
}
|
||||
|
||||
var aLength = a.length;
|
||||
var bLength = b.length;
|
||||
|
||||
if (aLength - bLength > threshold) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
var rows = this._rows;
|
||||
|
||||
for (var j = 0; j <= bLength; j++) {
|
||||
rows[0][j] = j;
|
||||
}
|
||||
|
||||
for (var i = 1; i <= aLength; i++) {
|
||||
var upRow = rows[(i - 1) % 3];
|
||||
var currentRow = rows[i % 3];
|
||||
var smallestCell = currentRow[0] = i;
|
||||
|
||||
for (var _j = 1; _j <= bLength; _j++) {
|
||||
var cost = a[i - 1] === b[_j - 1] ? 0 : 1;
|
||||
var currentCell = Math.min(upRow[_j] + 1, // delete
|
||||
currentRow[_j - 1] + 1, // insert
|
||||
upRow[_j - 1] + cost // substitute
|
||||
);
|
||||
|
||||
if (i > 1 && _j > 1 && a[i - 1] === b[_j - 2] && a[i - 2] === b[_j - 1]) {
|
||||
// transposition
|
||||
var doubleDiagonalCell = rows[(i - 2) % 3][_j - 2];
|
||||
currentCell = Math.min(currentCell, doubleDiagonalCell + 1);
|
||||
}
|
||||
|
||||
if (currentCell < smallestCell) {
|
||||
smallestCell = currentCell;
|
||||
}
|
||||
|
||||
currentRow[_j] = currentCell;
|
||||
} // Early exit, since distance can't go smaller than smallest element of the previous row.
|
||||
|
||||
|
||||
if (smallestCell > threshold) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
var distance = rows[aLength % 3][bLength];
|
||||
return distance <= threshold ? distance : undefined;
|
||||
};
|
||||
|
||||
return LexicalDistance;
|
||||
}();
|
||||
|
||||
function stringToArray(str) {
|
||||
var strLength = str.length;
|
||||
var array = new Array(strLength);
|
||||
|
||||
for (var i = 0; i < strLength; ++i) {
|
||||
array[i] = str.charCodeAt(i);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = toObjMap;
|
||||
|
||||
var _objectEntries3 = _interopRequireDefault(require("../polyfills/objectEntries.js"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
function toObjMap(obj) {
|
||||
/* eslint-enable no-redeclare */
|
||||
if (Object.getPrototypeOf(obj) === null) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
var map = Object.create(null);
|
||||
|
||||
for (var _i2 = 0, _objectEntries2 = (0, _objectEntries3.default)(obj); _i2 < _objectEntries2.length; _i2++) {
|
||||
var _ref2 = _objectEntries2[_i2];
|
||||
var key = _ref2[0];
|
||||
var value = _ref2[1];
|
||||
map[key] = value;
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
// @flow strict
|
||||
import objectEntries from '../polyfills/objectEntries';
|
||||
|
||||
import type {
|
||||
ObjMap,
|
||||
ObjMapLike,
|
||||
ReadOnlyObjMap,
|
||||
ReadOnlyObjMapLike,
|
||||
} from './ObjMap';
|
||||
|
||||
/* eslint-disable no-redeclare */
|
||||
declare function toObjMap<T>(obj: ObjMapLike<T>): ObjMap<T>;
|
||||
declare function toObjMap<T>(obj: ReadOnlyObjMapLike<T>): ReadOnlyObjMap<T>;
|
||||
|
||||
export default function toObjMap(obj) {
|
||||
/* eslint-enable no-redeclare */
|
||||
if (Object.getPrototypeOf(obj) === null) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
const map = Object.create(null);
|
||||
for (const [key, value] of objectEntries(obj)) {
|
||||
map[key] = value;
|
||||
}
|
||||
return map;
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
import objectEntries from "../polyfills/objectEntries.mjs";
|
||||
export default function toObjMap(obj) {
|
||||
/* eslint-enable no-redeclare */
|
||||
if (Object.getPrototypeOf(obj) === null) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
var map = Object.create(null);
|
||||
|
||||
for (var _i2 = 0, _objectEntries2 = objectEntries(obj); _i2 < _objectEntries2.length; _i2++) {
|
||||
var _ref2 = _objectEntries2[_i2];
|
||||
var key = _ref2[0];
|
||||
var value = _ref2[1];
|
||||
map[key] = value;
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
Reference in New Issue
Block a user