本文整理汇总了TypeScript中@sindresorhus/is.string函数的典型用法代码示例。如果您正苦于以下问题:TypeScript string函数的具体用法?TypeScript string怎么用?TypeScript string使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了string函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: constructor
/**
* Creates a Debuggee service object.
* @ref https://cloud.google.com/debugger/api/reference/rest/v2/Debuggee
*
* @param {object} properties - an object with properties to use for Debuggee
* initialization.
* @param {object} properties.project - Google Cloud Project ID
* @param {string} properties.uniquifier - Debuggee uniquifier within the
* project. Any string that identifies the application within the project
* can be used. Including environment and version or build IDs is
* recommended.
* @param {string} properties.description - A user specified string identifying
* this debuggable instance.
* @param {?string} properties.agentVersion - version ID of the agent. (default:
* the version of this module)
* @param {?object} labels - a set of custom properties about the debuggee that
* are reported to the service.
* @param {?array<object>} properties.sourceContexts
* @param {?StatusMessage} properties.statusMessage - A error string to register
* this as an erroring debuggable instance. This is useful if we have a
* problem starting the debugger support, and want to report to the API so
* that the user has a way of noticing.
* TODO(ofrobots): has this been renamed to `status` in the API?
*/
constructor(properties: DebuggeeProperties) {
if (!(this instanceof Debuggee)) {
return new Debuggee(properties);
}
properties = properties || {};
if (!is.string(properties.project)) {
throw new Error('properties.project must be a string');
}
if (!is.string(properties.uniquifier)) {
throw new Error('properties.uniquifier must be a string');
}
if (!is.string(properties.description)) {
throw new Error('properties.description must be a string');
}
this.project = properties.project;
this.uniquifier = properties.uniquifier;
this.description = properties.description;
this.agentVersion = properties.agentVersion;
if (properties.labels) {
this.labels = properties.labels;
}
if (properties.sourceContexts) {
this.sourceContexts = properties.sourceContexts;
}
if (properties.statusMessage) {
this.statusMessage = properties.statusMessage;
}
}
示例2: URLSearchParams
export default function merge<Target extends {[key: string]: unknown}, Source extends {[key: string]: unknown}>(target: Target, ...sources: Source[]): Target & Source {
for (const source of sources) {
for (const [key, sourceValue] of Object.entries(source)) {
if (is.undefined(sourceValue)) {
continue;
}
const targetValue = target[key];
if (targetValue instanceof URLSearchParams && sourceValue instanceof URLSearchParams) {
const params = new URLSearchParams();
const append = (value: string, key: string) => params.append(key, value);
targetValue.forEach(append);
sourceValue.forEach(append);
target[key] = params;
} else if (is.urlInstance(targetValue) && (is.urlInstance(sourceValue) || is.string(sourceValue))) {
target[key] = new URL(sourceValue as string, targetValue);
} else if (is.plainObject(sourceValue)) {
if (is.plainObject(targetValue)) {
target[key] = merge({}, targetValue, sourceValue);
} else {
target[key] = merge({}, sourceValue);
}
} else if (is.array(sourceValue)) {
target[key] = merge([], sourceValue);
} else {
target[key] = sourceValue;
}
}
}
return target as Target & Source;
}
示例3: async
export default async (options: any): Promise<number | undefined> => {
const {body} = options;
if (options.headers['content-length']) {
return Number(options.headers['content-length']);
}
if (!body && !options.stream) {
return 0;
}
if (is.string(body)) {
return Buffer.byteLength(body);
}
if (isFormData(body)) {
return promisify(body.getLength.bind(body))();
}
if (body instanceof fs.ReadStream) {
const {size} = await promisify(fs.stat)(body.path);
return size;
}
return undefined;
};
示例4: default
export default (url: any): URLOptions => {
const options: URLOptions = {
protocol: url.protocol,
hostname: url.hostname.startsWith('[') ? url.hostname.slice(1, -1) : url.hostname,
host: url.host,
hash: url.hash,
search: url.search,
pathname: url.pathname,
href: url.href,
path: is.null_(url.search) ? url.pathname : `${url.pathname}${url.search}`
};
if (is.string(url.port) && url.port.length > 0) {
options.port = Number(url.port);
}
if (url.username || url.password) {
options.auth = `${url.username}:${url.password}`;
}
return options;
};
示例5: async
const fetchFromAttachmentsIndex = async ({
name,
conversationId,
WhisperMessageCollection,
count,
}: {
name: 'hasVisualMediaAttachments' | 'hasFileAttachments';
conversationId: string;
WhisperMessageCollection: BackboneCollection<Message>;
count: number;
}): Promise<Array<Message>> => {
if (!is.string(conversationId)) {
throw new TypeError("'conversationId' is required");
}
if (!is.object(WhisperMessageCollection)) {
throw new TypeError("'WhisperMessageCollection' is required");
}
const collection = new WhisperMessageCollection();
const lowerReceivedAt = 0;
const upperReceivedAt = Number.MAX_VALUE;
const condition: IndexableBoolean = 1;
await deferredToPromise(
collection.fetch({
index: {
name,
lower: [conversationId, lowerReceivedAt, condition],
upper: [conversationId, upperReceivedAt, condition],
order: 'desc',
},
limit: count,
})
);
return collection.models.map(model => model.toJSON());
};
示例6: setImmediate
setImmediate(async () => {
try {
for (const hook of options.hooks.beforeRequest) {
// eslint-disable-next-line no-await-in-loop
await hook(options);
}
// Serialize body
const {body, headers} = options;
const isForm = !is.nullOrUndefined(options.form);
const isJSON = !is.nullOrUndefined(options.json);
const isBody = !is.nullOrUndefined(body);
if ((isBody || isForm || isJSON) && withoutBody.has(options.method)) {
throw new TypeError(`The \`${options.method}\` method cannot be used with a body`);
}
if (isBody) {
if (isForm || isJSON) {
throw new TypeError('The `body` option cannot be used with the `json` option or `form` option');
}
if (is.object(body) && 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 (!is.nodeStream(body) && !is.string(body) && !is.buffer(body)) {
throw new TypeError('The `body` option must be a stream.Readable, string, Buffer, Object or Array');
}
} else if (isForm) {
if (!is.object(options.form)) {
throw new TypeError('The `form` option must be an Object');
}
headers['content-type'] = headers['content-type'] || 'application/x-www-form-urlencoded';
options.body = (new URLSearchParams(options.form)).toString();
} else if (isJSON) {
headers['content-type'] = headers['content-type'] || 'application/json';
options.body = JSON.stringify(options.json);
}
// Convert buffer to stream to receive upload progress events (#322)
if (is.buffer(body)) {
options.body = toReadableStream(body);
uploadBodySize = body.length;
} else {
uploadBodySize = await getBodySize(options);
}
if (is.undefined(headers['content-length']) && is.undefined(headers['transfer-encoding'])) {
if ((uploadBodySize > 0 || options.method === 'PUT') && !is.undefined(uploadBodySize)) {
headers['content-length'] = uploadBodySize;
}
}
if (!options.stream && options.responseType === 'json' && is.undefined(headers.accept)) {
options.headers.accept = 'application/json';
}
requestUrl = options.href || (new URL(options.path, urlLib.format(options))).toString();
await get(options);
} catch (error) {
emitError(error);
}
});
示例7: merge
export const normalizeArguments = (url, options, defaults?: any) => {
if (is.plainObject(url)) {
options = {...url, ...options};
url = options.url || '';
delete options.url;
}
if (defaults) {
options = merge({}, defaults.options, options ? preNormalizeArguments(options, defaults.options) : {});
} else {
options = merge({}, preNormalizeArguments(options));
}
if (!is.string(url) && !is.object(url)) {
throw new TypeError(`Parameter \`url\` must be a string or object, not ${is(url)}`);
}
if (is.string(url) && !(url === '' && !options.baseUrl)) {
if (options.baseUrl) {
if (url.startsWith('/')) {
url = url.slice(1);
}
} else {
url = url.replace(/^unix:/, 'http://$&');
}
url = urlToOptions(new URL(url, options.baseUrl));
} else if (is(url) === 'URL') {
url = urlToOptions(url);
}
// Override both null/undefined with default protocol
options = merge({path: ''}, url, {protocol: url.protocol || 'https:'}, options);
for (const hook of options.hooks.init) {
const called = hook(options);
if (is.promise(called)) {
throw new TypeError('The `init` hook must be a synchronous function');
}
}
const {baseUrl} = options;
Object.defineProperty(options, 'baseUrl', {
set: () => {
throw new Error('Failed to set baseUrl. Options are normalized already.');
},
get: () => baseUrl
});
let {searchParams} = options;
delete options.searchParams;
if (options.query) {
if (!shownDeprecation) {
console.warn('`options.query` is deprecated. We support it solely for compatibility - it will be removed in Got 11. Use `options.searchParams` instead.');
shownDeprecation = true;
}
searchParams = options.query;
delete options.query;
}
// TODO: This should be used in the `options` type instead
interface SearchParams {
[key: string]: string | number | boolean | null;
}
if (is.nonEmptyString(searchParams) || is.nonEmptyObject(searchParams) || searchParams instanceof URLSearchParams) {
if (!is.string(searchParams)) {
if (!(searchParams instanceof URLSearchParams)) {
validateSearchParams(searchParams);
searchParams = searchParams as SearchParams;
}
searchParams = (new URLSearchParams(searchParams)).toString();
}
options.path = `${options.path.split('?')[0]}?${searchParams}`;
}
if (options.hostname === 'unix') {
const matches = /(.+?):(.+)/.exec(options.path);
if (matches) {
const [, socketPath, path] = matches;
options = {
...options,
socketPath,
path,
host: null
};
}
}
const {headers} = options;
for (const [key, value] of Object.entries(headers)) {
if (is.nullOrUndefined(value)) {
delete headers[key];
}
//.........这里部分代码省略.........
示例8: TypeError
const verify = (value: unknown, type: string) => {
if (!is.string(value) && !is.number(value) && !is.boolean(value) && !is.null_(value)) {
throw new TypeError(`The \`searchParams\` ${type} '${value}' must be a string, number, boolean or null`);
}
};