How to use @sindresorhus/is - 10 common examples

To help you get started, we’ve selected a few @sindresorhus/is 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 public-transport / hafas-client / test / lib / validate-line-without-mode.js View on Github external
a.strictEqual(typeof line.name, 'string', name + '.name must be a string')
	a.ok(line.name.length > 0, name + '.name can\'t be empty')

	// skipping line validation here
	// see https://github.com/derhuerst/hafas-client/issues/8#issuecomment-355839965
	if (is.undefined(line.mode) || is.null(line.mode)) {
		console.error(`ÖBB: Missing \`mode\` for line ${line.name} (at ${name}).`)
	}

	if (!is.undefined(line.subMode)) {
	a.fail(name + '.subMode is reserved an should not be used for now')
	}

	// todo: routes

	if (!is.null(line.operator) && !is.undefined(line.operator)) {
		validate(['operator'], line.operator, name + '.operator')
	}
}
github public-transport / hafas-client / test / lib / validate-line-without-mode.js View on Github external
const validateLineWithoutMode = (validate, line, name) => {
	validateItem(line, name)

	a.strictEqual(line.type, 'line', name + '.type must be `line`')

	validateReference(line.id, name + '.id')

	a.strictEqual(typeof line.name, 'string', name + '.name must be a string')
	a.ok(line.name.length > 0, name + '.name can\'t be empty')

	// skipping line validation here
	// see https://github.com/derhuerst/hafas-client/issues/8#issuecomment-355839965
	if (is.undefined(line.mode) || is.null(line.mode)) {
		console.error(`ÖBB: Missing \`mode\` for line ${line.name} (at ${name}).`)
	}

	if (!is.undefined(line.subMode)) {
	a.fail(name + '.subMode is reserved an should not be used for now')
	}

	// todo: routes

	if (!is.null(line.operator) && !is.undefined(line.operator)) {
		validate(['operator'], line.operator, name + '.operator')
	}
}
github sindresorhus / got / test / create.ts View on Github external
server.get('/', echoHeaders);

	const instance = got.extend({
		handlers: [
			async (options, next) => {
				const result = await next(options);
				// @ts-ignore Manual tests
				result.modified = true;

				return result;
			}
		]
	});

	const promise = instance('');
	t.true(is.function_(promise.cancel));
	// @ts-ignore Manual tests
	t.true((await promise).modified);
});
github Hackdromeda / BugBrowser / node_modules / got / index.js View on Github external
}

		opts.path = `${opts.path.split('?')[0]}?${opts.query}`;
		delete opts.query;
	}

	if (opts.json && is.undefined(opts.headers.accept)) {
		opts.headers.accept = 'application/json';
	}

	const body = opts.body;
	if (is.nullOrUndefined(body)) {
		opts.method = (opts.method || 'GET').toUpperCase();
	} else {
		const headers = opts.headers;
		if (!is.nodeStream(body) && !is.string(body) && !is.buffer(body) && !(opts.form || opts.json)) {
			throw new TypeError('The `body` option must be a stream.Readable, string, Buffer or plain Object');
		}

		const canBodyBeStringified = is.plainObject(body) || is.array(body);
		if ((opts.form || opts.json) && !canBodyBeStringified) {
			throw new TypeError('The `body` option must be a plain Object or Array when the `form` or `json` option is used');
		}

		if (isFormData(body)) {
			// Special case for https://github.com/form-data/form-data
			headers['content-type'] = headers['content-type'] || `multipart/form-data; boundary=${body.getBoundary()}`;
		} else if (opts.form && canBodyBeStringified) {
			headers['content-type'] = headers['content-type'] || 'application/x-www-form-urlencoded';
			opts.body = querystring.stringify(body);
		} else if (opts.json && canBodyBeStringified) {
			headers['content-type'] = headers['content-type'] || 'application/json';
github htaussig / ProcProj / canvasSketch / future3D / node_modules / got / source / request-as-event-emitter.js View on Github external
module.exports = (options, input) => {
	const emitter = new EventEmitter();
	const redirects = [];
	let currentRequest;
	let requestUrl;
	let redirectString;
	let uploadBodySize;
	let retryCount = 0;
	let shouldAbort = false;

	const setCookie = options.cookieJar ? util.promisify(options.cookieJar.setCookie.bind(options.cookieJar)) : null;
	const getCookieString = options.cookieJar ? util.promisify(options.cookieJar.getCookieString.bind(options.cookieJar)) : null;
	const agents = is.object(options.agent) ? options.agent : null;

	const emitError = async error => {
		try {
			for (const hook of options.hooks.beforeError) {
				// eslint-disable-next-line no-await-in-loop
				error = await hook(error);
			}

			emitter.emit('error', error);
		} catch (error2) {
			emitter.emit('error', error2);
		}
	};

	const get = async options => {
		const currentUrl = redirectString || requestUrl;
github sx1989827 / DOClever / node_modules / got / index.js View on Github external
if (query) {
		if (!is.string(query)) {
			opts.query = querystring.stringify(query);
		}

		opts.path = `${opts.path.split('?')[0]}?${opts.query}`;
		delete opts.query;
	}

	if (opts.json && is.undefined(opts.headers.accept)) {
		opts.headers.accept = 'application/json';
	}

	const body = opts.body;
	if (is.nullOrUndefined(body)) {
		opts.method = (opts.method || 'GET').toUpperCase();
	} else {
		const headers = opts.headers;
		if (!is.nodeStream(body) && !is.string(body) && !is.buffer(body) && !(opts.form || opts.json)) {
			throw new TypeError('The `body` option must be a stream.Readable, string, Buffer or plain Object');
		}

		const canBodyBeStringified = is.plainObject(body) || is.array(body);
		if ((opts.form || opts.json) && !canBodyBeStringified) {
			throw new TypeError('The `body` option must be a plain Object or Array when the `form` or `json` option is used');
		}

		if (isFormData(body)) {
			// Special case for https://github.com/form-data/form-data
			headers['content-type'] = headers['content-type'] || `multipart/form-data; boundary=${body.getBoundary()}`;
		} else if (opts.form && canBodyBeStringified) {
github sx1989827 / DOClever / node_modules / got / index.js View on Github external
}

		opts.path = `${opts.path.split('?')[0]}?${opts.query}`;
		delete opts.query;
	}

	if (opts.json && is.undefined(opts.headers.accept)) {
		opts.headers.accept = 'application/json';
	}

	const body = opts.body;
	if (is.nullOrUndefined(body)) {
		opts.method = (opts.method || 'GET').toUpperCase();
	} else {
		const headers = opts.headers;
		if (!is.nodeStream(body) && !is.string(body) && !is.buffer(body) && !(opts.form || opts.json)) {
			throw new TypeError('The `body` option must be a stream.Readable, string, Buffer or plain Object');
		}

		const canBodyBeStringified = is.plainObject(body) || is.array(body);
		if ((opts.form || opts.json) && !canBodyBeStringified) {
			throw new TypeError('The `body` option must be a plain Object or Array when the `form` or `json` option is used');
		}

		if (isFormData(body)) {
			// Special case for https://github.com/form-data/form-data
			headers['content-type'] = headers['content-type'] || `multipart/form-data; boundary=${body.getBoundary()}`;
		} else if (opts.form && canBodyBeStringified) {
			headers['content-type'] = headers['content-type'] || 'application/x-www-form-urlencoded';
			opts.body = querystring.stringify(body);
		} else if (opts.json && canBodyBeStringified) {
			headers['content-type'] = headers['content-type'] || 'application/json';
github sindresorhus / got / source / normalize-arguments.ts View on Github external
// Normalize URL
	// TODO: drop `optionsToUrl` in Got 12
	if (is.string(options.url)) {
		options.url = (options.prefixUrl as string) + options.url;
		options.url = options.url.replace(/^unix:/, 'http://$&');

		if (options.searchParams || options.search) {
			options.url = options.url.split('?')[0];
		}

		options.url = optionsToUrl({
			origin: options.url,
			...options
		});
	} else if (!is.urlInstance(options.url)) {
		options.url = optionsToUrl({origin: options.prefixUrl as string, ...options});
	}

	const normalizedOptions = options as NormalizedOptions;

	// Make it possible to change `options.prefixUrl`
	let prefixUrl = options.prefixUrl as string;
	Object.defineProperty(normalizedOptions, 'prefixUrl', {
		set: (value: string) => {
			if (!normalizedOptions.url.href.startsWith(value)) {
				throw new Error(`Cannot change \`prefixUrl\` from ${prefixUrl} to ${value}: ${normalizedOptions.url.href}`);
			}

			normalizedOptions.url = new URL(value + normalizedOptions.url.href.slice(prefixUrl.length));
			prefixUrl = value;
		},
github sx1989827 / DOClever / node_modules / got / index.js View on Github external
throw new TypeError('The `body` option must be a plain Object or Array when the `form` or `json` option is used');
		}

		if (isFormData(body)) {
			// Special case for https://github.com/form-data/form-data
			headers['content-type'] = headers['content-type'] || `multipart/form-data; boundary=${body.getBoundary()}`;
		} else if (opts.form && canBodyBeStringified) {
			headers['content-type'] = headers['content-type'] || 'application/x-www-form-urlencoded';
			opts.body = querystring.stringify(body);
		} else if (opts.json && canBodyBeStringified) {
			headers['content-type'] = headers['content-type'] || 'application/json';
			opts.body = JSON.stringify(body);
		}

		if (is.undefined(headers['content-length']) && is.undefined(headers['transfer-encoding']) && !is.nodeStream(body)) {
			const length = is.string(opts.body) ? Buffer.byteLength(opts.body) : opts.body.length;
			headers['content-length'] = length;
		}

		// Convert buffer to stream to receive upload progress events
		// see https://github.com/sindresorhus/got/pull/322
		if (is.buffer(body)) {
			opts.body = intoStream(body);
			opts.body._buffer = body;
		}

		opts.method = (opts.method || 'POST').toUpperCase();
	}

	if (opts.hostname === 'unix') {
		const matches = /(.+?):(.+)/.exec(opts.path);
github deltachat / deltachat-desktop / conversations / ts / components / conversation / Emojify.tsx View on Github external
function getImageTag({
  match,
  sizeClass,
  key,
}: {
  match: any;
  sizeClass: string | undefined;
  key: string | number;
}) {
  const result = getReplacementData(match[0], match[1], match[2]);

  if (is.string(result)) {
    return <span>{match[0]}</span>;
  }

  const img = findImage(result.value, result.variation);
  const title = getTitle(result.value);

  return (
    // tslint:disable-next-line react-a11y-img-has-alt
    <img title="{`:${title}:`}" data-codepoints="{img.full_idx}" is="" alt="" src="{img.path}">