本文整理汇总了TypeScript中lodash.memoize函数的典型用法代码示例。如果您正苦于以下问题:TypeScript memoize函数的具体用法?TypeScript memoize怎么用?TypeScript memoize使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了memoize函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: getSuggestionsProvider
export function getSuggestionsProvider(
config: { get: (key: string) => any },
fetch: (...options: any[]) => any
) {
const requestSuggestions = memoize(
(index: string, field: Field, query: string, boolFilter: any = []) => {
return fetch({
pathname: `/api/kibana/suggestions/values/${index}`,
method: 'POST',
body: JSON.stringify({ query, field: field.name, boolFilter }),
});
},
resolver
);
return async (index: string, field: Field, query: string, boolFilter?: any) => {
const shouldSuggestValues = config.get('filterEditor:suggestValues');
if (field.type === 'boolean') {
return [true, false];
} else if (!shouldSuggestValues || !field.aggregatable || field.type !== 'string') {
return [];
}
return await requestSuggestions(index, field, query, boolFilter);
};
}
示例2: memoize
export const createMockServer = ({ settings = {} }: any) => {
const mockServer = {
expose: () => {
' ';
},
config: memoize(() => ({ get: jest.fn() })),
info: {
protocol: 'http',
},
plugins: {
elasticsearch: {
getCluster: memoize(() => {
return {
callWithRequest: jest.fn(),
};
}),
},
},
savedObjects: {
getScopedSavedObjectsClient: jest.fn(),
},
uiSettingsServiceFactory: jest.fn().mockReturnValue({ get: jest.fn() }),
};
const defaultSettings: any = {
'xpack.reporting.encryptionKey': 'testencryptionkey',
'server.basePath': '/sbp',
'server.host': 'localhost',
'server.port': 5601,
'xpack.reporting.kibanaServer': {},
};
mockServer.config().get.mockImplementation((key: any) => {
return key in settings ? settings[key] : defaultSettings[key];
});
return mockServer;
};
示例3: groupByNestedPropFilter
export function groupByNestedPropFilter() {
'ngInject';
return _.memoize(filter, resolver);
function filter(input: any, property: string) {
if (angular.isUndefined(property) || angular.isUndefined(input)) { return input; }
return _.groupBy(input, property);
}
function resolver(input: any, property: string) {
return angular.toJson(input) + property;
}
}
示例4: memoize
import * as execa from 'execa'
import { memoize } from 'lodash'
const exec = args => () => execa.shellSync(args.trim()).stdout
const mexec = memoize(exec)
export const gitRoot = mexec(`
git rev-parse --show-toplevel
`)
export const gitShortHash = mexec(`
git rev-parse --short HEAD
`)
示例5: groupSections
export function groupSections(sysInfoData: T.SysInfoValueObject) {
const mainSection: T.SysInfoValueObject = {};
const sections: T.SysInfoSection = {};
each(sysInfoData, (item, key) => {
if (typeof item !== 'object' || item instanceof Array) {
mainSection[key] = item;
} else {
sections[key] = item;
}
});
return { mainSection, sections };
}
export const parseQuery = memoize(
(urlQuery: RawQuery): Query => ({
expandedCards: parseAsArray(urlQuery.expand, parseAsString)
})
);
export const serializeQuery = memoize(
(query: Query): RawQuery =>
cleanQuery({
expand: serializeStringArray(query.expandedCards)
})
);
export function sortUpgrades(upgrades: T.SystemUpgrade[]): T.SystemUpgrade[] {
return sortBy(upgrades, [
(upgrade: T.SystemUpgrade) => -Number(upgrade.version.split('.')[0]),
(upgrade: T.SystemUpgrade) => -Number(upgrade.version.split('.')[1] || 0),
(upgrade: T.SystemUpgrade) => -Number(upgrade.version.split('.')[2] || 0)
示例6: memoize
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { memoize } from 'lodash';
const cache = memoize((s: string) => {
return new RegExp('^' + s.replace(/./g, (x) => {
return /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/.test(x) ? '\\' + x + '?' : x + '?';
}) + '$', 'i');
});
export default (s: string, pattern: string) => {
return cache(s).test(pattern);
};
示例7: memoize
return false;
}
for (let i = 0; i < pathTokens.length; i++) {
if (pathTokens[i] !== splatTokens[i]) {
return false;
}
}
return true;
};
export const parseQuery = memoize(
(urlQuery: RawQuery): Query => ({
search: parseAsString(urlQuery['query']),
deprecated: parseAsOptionalBoolean(urlQuery['deprecated']) || false,
internal: parseAsOptionalBoolean(urlQuery['internal']) || false
})
);
export const serializeQuery = memoize(
(query: Partial<Query>): RawQuery =>
cleanQuery({
query: query.search ? serializeString(query.search) : undefined,
deprecated: query.deprecated || undefined,
internal: query.internal || undefined
})
);
export function parseVersion(version: string) {
const match = /(\d+)\.(\d+)/.exec(version);
示例8: memoize
asDecimal,
asMillis,
asPercent,
tpmUnit
} from '../../utils/formatters';
import { IUrlParams } from '../urlParams';
export const getEmptySerie = memoize(
(start = Date.now() - 3600000, end = Date.now()) => {
const dates = d3.time
.scale()
.domain([new Date(start), new Date(end)])
.ticks();
return [
{
data: dates.map(x => ({
x: x.getTime(),
y: 1
}))
}
];
},
(start: number, end: number) => [start, end].join('_')
);
interface IEmptySeries {
data: Coordinate[];
}
export interface ITpmBucket {
title: string;
示例9: memoize
RawQuery,
parseAsBoolean,
serializeOptionalBoolean,
parseAsOptionalString,
serializeString
} from '../../../helpers/query';
export interface Query {
error?: string;
manual: boolean;
organization?: string;
}
export const parseQuery = memoize(
(urlQuery: RawQuery): Query => {
return {
error: parseAsOptionalString(urlQuery['error']),
manual: parseAsBoolean(urlQuery['manual'], false),
organization: parseAsOptionalString(urlQuery['organization'])
};
}
);
export const serializeQuery = memoize(
(query: Query): RawQuery =>
cleanQuery({
manual: serializeOptionalBoolean(query.manual || undefined),
organization: serializeString(query.organization)
})
);
示例10: memoize
default:
return EmojiStyleCode.Facebook30;
}
}
/**
Renders the given emoji in the renderer process and returns a PNG `data:` URL.
*/
const renderEmoji = memoize(
async (emoji: string): Promise<string> =>
new Promise(resolve => {
const listener = (_event: ElectronEvent, arg: {emoji: string; dataUrl: string}): void => {
if (arg.emoji !== emoji) {
return;
}
ipcMain.removeListener('native-emoji', listener);
resolve(arg.dataUrl);
};
ipcMain.on('native-emoji', listener);
sendBackgroundAction('render-native-emoji', emoji);
})
);
/**
@param url - A Facebook emoji URL like `https://static.xx.fbcdn.net/images/emoji.php/v9/tae/2/16/1f471_1f3fb_200d_2640.png`.
*/
function urlToEmoji(url: string): string {
const codePoints = url
.split('/')
.pop()!