本文整理汇总了TypeScript中ramda.groupBy函数的典型用法代码示例。如果您正苦于以下问题:TypeScript groupBy函数的具体用法?TypeScript groupBy怎么用?TypeScript groupBy使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了groupBy函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1:
export const unnest = (categories: Category[]): Category[] => {
const children = R.groupBy(R.pathOr('root', ['parent', 'name']), categories);
return children.root.map(category => ({
...category,
children: children[category.name] || []
}))
}
示例2: async
export default async () => {
const pages = await fetch(`${host}${basename}/page`, options).then(res => res.json());
const pages$ = await Promise.all(
pages.map((page: any) => fetch(
`${host}${basename}/page/${page.alias}`,
options,
)
.then(res => res.json())
)
);
const categories = await fetch(`${host}${basename}/category`, options).then(res => res.json());
const categories$ = await Promise.all(
categories.map((category: any) => fetch(
`${host}${basename}/category/${category.name}`,
options,
)
.then(res => res.json())
.catch(e=> {
console.log(e);
})
)
);
const photos = await Promise.all(
categories.map((category: any) => fetch(
`${host}${basename}/category/${category.name}/photo`,
options,
)
.then(res => res.json())
.catch(() => [])
.then(photos => photos.map((photo: any) => ({
...photo,
category: category.name,
})))
)
);
const photos$ = R.pipe(
R.unnest,
R.groupBy(R.prop('id')),
// @ts-ignore
R.map((photoList: Photo[]) => ({
...R.head(photoList),
category: R.map(R.prop('category'), photoList)
})),
R.values,
)(photos);
return {
pages: pages$.filter(Boolean),
categories: categories$.filter(Boolean),
photos: photos$.filter(Boolean),
} as any;
};
示例3: listMigrations
export function listMigrations(directory: string): Promise<Migration[]> {
return fs.readdirAsync(directory)
.map(R.match(MIGRATION_FILE_REGEXP))
.filter(R.identity)
.then(R.groupBy(R.nth<string>(1)))
.then(R.mapObj(R.partial(matchesToMigration, directory)))
.then(R.values)
.then(R.sortBy((migration: Migration) => migration.id));
}
示例4: sort
public static sort(unsorted: number [] = []): number[] {
if (unsorted.length !== 0) {
const sorted = [];
const m = R.head(unsorted);
const {l, h} = R.groupBy(curr => R.gte(curr, m) ? 'h' : 'l', R.drop(1, unsorted))
sorted.push(...QuickSort.sort(l));
sorted.push(m);
sorted.push(...QuickSort.sort(h));
return sorted;
} else
return unsorted;
}
示例5: parseCandidateAnswers
function parseCandidateAnswers(fileContent: string): Candidate[] {
const rows: string[][] = parse(fileContent);
const rowsByCandidate = R.groupBy(row => row[AnswerFileCols.NAME], rows.slice(1));
const candidateRowPairs: [string, Row[]][] = R.toPairs(rowsByCandidate) as any;
return candidateRowPairs.map((candidateAndAnswerRows: [string, Row[]]) => {
const candidateName = candidateAndAnswerRows[0];
const answerRows = candidateAndAnswerRows[1];
const oneRow = answerRows[0];
return {
answers: getAnswerMapFromRows(answerRows),
id: candidateName,
name: candidateName,
party: oneRow[AnswerFileCols.PARTY],
reasons: getReasonsMapFromRows(answerRows),
regions: oneRow[AnswerFileCols.REGION].split(/,\s+/)
};
});
}
示例6: groupBy
const groupByLength = (
w: string[],
): {
[key: number]: string[];
} => groupBy(length as any, w as any) as any;
示例7: isByLinode
const isDeprecated = (i: Linode.Image) => i.deprecated === true;
const isRecommended = (i: Linode.Image) => isByLinode(i) && !isDeprecated(i);
const isOlderImage = (i: Linode.Image) => isByLinode(i) && isDeprecated(i);
interface GroupedImages {
deleted?: Linode.Image[];
recommended?: Linode.Image[];
older?: Linode.Image[];
images?: Linode.Image[];
}
export let groupImages: (i: Linode.Image[]) => GroupedImages;
groupImages = groupBy(
cond([
[isRecentlyDeleted, always('deleted')],
[isRecommended, always('recommended')],
[isOlderImage, always('older')],
[(i: Linode.Image) => true, always('images')]
])
);
export const groupNameMap = {
_default: 'Other',
deleted: 'Recently Deleted Disks',
recommended: '64-bit Distributions - Recommended',
older: 'Older Distributions',
images: 'Images'
};
export const getDisplayNameForGroup = (key: string) =>
propOr('Other', key, groupNameMap);