本文整理汇总了TypeScript中lodash.chunk函数的典型用法代码示例。如果您正苦于以下问题:TypeScript chunk函数的具体用法?TypeScript chunk怎么用?TypeScript chunk使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了chunk函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: getObjects
/**
* Load all objects with given uris
* (use bulk loading instead of getting objects one by one)
*
* @method getObjects
* @param {String} projectId id of the project
* @param {Array} objectUris array of uris for objects to be loaded
* @return {Array} array of loaded elements
*/
public getObjects(projectId: string, objectUris: string[]): any {
const LIMIT = 50;
const uri = `/gdc/md/${projectId}/objects/get`;
const objectsUrisChunks = chunk(objectUris, LIMIT);
const promises = objectsUrisChunks.map((objectUrisChunk) => {
const body = {
get: {
items: objectUrisChunk
}
};
return this.xhr.post(uri, { body })
.then((r: ApiResponse) => {
if (!r.response.ok) {
throw new ApiResponseError(r.response.statusText, r.response, r.responseBody);
}
return r.getData();
})
.then((result: any) =>
_get(result, ['objects', 'items']).map((item: any) => {
if (item.visualizationObject) {
return {
visualizationObject: convertReferencesToUris(item.visualizationObject)
};
}
return item;
}));
});
return Promise.all(promises).then(flatten);
}
示例2: mergeAverageDownloadsPerDay
mergeAverageDownloadsPerDay(packages, (error, packages) => {
if (error) return callback(error);
logger.debug('updating with %d packages (%s)', packages.length, updates_only ? 'updates only' : 'all packages');
var batches = _.chunk(packages, 500);
async.eachSeries(batches, insertPackages, callback);
});
示例3: dataExport
async function dataExport() {
await db.connect()
const tmpFile = "/tmp/owid_chartdata.sql"
const variablesToExportQuery = `
SELECT DISTINCT cd.variableId FROM chart_dimensions cd
WHERE NOT EXISTS (select * from tags t join chart_tags ct on ct.tagId = t.id where ct.chartId=cd.chartId and t.name='Private')
`
const variableIds = (await db.query(variablesToExportQuery)).map((row: any) => row.variableId)
console.log(`Exporting data for ${variableIds.length} variables to ${tmpFile}`)
await exec(`rm -f ${tmpFile}`)
let count = 0
for (const chunk of _.chunk(variableIds, 100)) {
await exec(`mysqldump --no-create-info ${DB_NAME} data_values --where="variableId IN (${chunk.join(",")})" >> ${tmpFile}`)
count += chunk.length
console.log(count)
}
await db.end()
}
示例4: _getBulkItems
async _getBulkItems(
itemList: D2ItemFetchRequest[],
platformSelection: number,
mode: number
): Promise<D2ItemFetchResponse[]> {
if (!itemList.length) {
return Promise.resolve<D2ItemFetchResponse[]>([]);
}
// DTR admins requested we only make requests in batches of 10, and not in parallel
const arrayOfArrays: D2ItemFetchRequest[][] = _.chunk(itemList, 10);
const results: D2ItemFetchResponse[] = [];
for (const arraySlice of arrayOfArrays) {
const promiseSlice = dtrFetch(
`https://db-api.destinytracker.com/api/external/reviews/fetch?platform=${platformSelection}&mode=${mode}`,
arraySlice
).then(handleD2Errors, handleD2Errors);
try {
loadingTracker.addPromise(promiseSlice);
const result = await promiseSlice;
results.push(...result);
} catch (error) {
console.error(error);
}
}
return results;
}
示例5: getTripPhases
export function getTripPhases(tripString: string): TripPhase[] {
const tripStages = tripString
.replace('\t', '')
.split('\n')
.map(line => line.trim())
.filter(line => line.length !== 0)
.filter(
line =>
line.startsWith('Depart') ||
line.startsWith('Board') ||
line.startsWith('Arrive')
)
const transfers = _.chunk(tripStages, 3)
return transfers.map(([departureStr, boardingStr, arrivalStr]) => {
const [from, dTime, dTimeAmPm] = departureRE.exec(departureStr)!!.slice(1)
const departureTimeStr = `${dTime} ${dTimeAmPm}`
const departure = { from: from.trim(), at: departureTimeStr }
const [trainNumber, towards] = boardRE.exec(boardingStr)!!.slice(1)
const board = { trainNumber, towards }
const [destination, aTime, aTimeAmPm] = arrivalRE
.exec(arrivalStr)!!
.slice(1)
const arrivalTimeStr = `${aTime} ${aTimeAmPm}`
const arrival = { destination: destination.trim(), at: arrivalTimeStr }
const duration = getDuration(departureTimeStr, arrivalTimeStr)
return { departure, board, arrival, duration }
})
}
示例6: createEnrollmentTokens
public async createEnrollmentTokens(
user: FrameworkUser,
numTokens: number = 1
): Promise<string[]> {
const tokens = [];
const enrollmentTokensTtlInSeconds = this.framework.getSetting('enrollmentTokensTtlInSeconds');
const enrollmentTokenExpiration = moment()
.add(enrollmentTokensTtlInSeconds, 'seconds')
.toJSON();
const enrollmentTokenSecret = this.framework.getSetting('encryptionKey');
while (tokens.length < numTokens) {
const tokenData = {
created: moment().toJSON(),
expires: enrollmentTokenExpiration,
randomHash: randomBytes(26).toString(),
};
tokens.push({
expires_on: enrollmentTokenExpiration,
token: signToken(tokenData, enrollmentTokenSecret),
});
}
await Promise.all(
chunk(tokens, 100).map(tokenChunk => this.adapter.insertTokens(user, tokenChunk))
);
return tokens.map(token => token.token);
}
示例7:
export async function batchAndCombine<T, K>(lookupIds: Array<K>, dataFetch: (chunkLookupIds: Array<K>) => Promise<Array<T>>) {
const chunkedIds = _.chunk(lookupIds, MAX_DB_CHUNK_SIZE);
const result: Array<Array<T>> = [];
for (const chunk of chunkedIds) result.push(await dataFetch(chunk));
const fullResults = _.flatten(result);
// sort results
// limit results
return fullResults;
}
示例8: sortColorsByHue
function sortColorsByHue(hexColors: string[]) {
const hslColors = _.map(hexColors, hexToHsl);
const sortedHSLColors = _.sortBy(hslColors, ['h']);
const chunkedHSLColors = _.chunk(sortedHSLColors, PALETTE_ROWS);
const sortedChunkedHSLColors = _.map(chunkedHSLColors, chunk => {
return _.sortBy(chunk, 'l');
});
const flattenedZippedSortedChunkedHSLColors = _.flattenDeep(_.zip(...sortedChunkedHSLColors));
return _.map(flattenedZippedSortedChunkedHSLColors, hslToHex);
}
示例9: sortColorsByHue
export function sortColorsByHue(hexColors) {
const hslColors = _.map(hexColors, hexToHsl);
let sortedHSLColors = _.sortBy(hslColors, ['h']);
sortedHSLColors = _.chunk(sortedHSLColors, PALETTE_ROWS);
sortedHSLColors = _.map(sortedHSLColors, chunk => {
return _.sortBy(chunk, 'l');
});
sortedHSLColors = _.flattenDeep(_.zip(...sortedHSLColors));
return _.map(sortedHSLColors, hslToHex);
}
示例10: parse
function parse(hex: string): number[] {
switch (hex.length) {
case 3:
case 4:
return hex.split('')
.map((n) => `${n}${n}`)
.map((n) => parseInt(n, 16));
case 6:
case 8:
return _.chunk(_.range(0, hex.length), 2)
.map(([i, j]) => `${hex[i]}${hex[j]}`)
.map((n) => parseInt(n, 16));
default:
// Invalid value was passed
return [];
}
}