本文整理汇总了TypeScript中ramda.findIndex函数的典型用法代码示例。如果您正苦于以下问题:TypeScript findIndex函数的具体用法?TypeScript findIndex怎么用?TypeScript findIndex使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了findIndex函数的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: findIndex
.reduceRight((updatedArray, el) => {
/**
* We need to update in place to maintain the correct timeline of events. Update in-place
* by finding the index then updating at that index.
*/
const indexOfFoundEvent = findIndex(({ id }) => id === el.id, updatedArray);
return indexOfFoundEvent > -1
? update(indexOfFoundEvent, el, updatedArray)
: [el, ...updatedArray];
}, prevArr)
示例2: sum
// sum :: [Number] → Number
sum(filed, list) {
return R.compose(R.sum, R.pluck(filed))(list)
},
// pluck :: Functor f => k → f {k: v} → f v
pluck(filed, list) {
return R.pluck(filed)(list)
},
// filter :: Filterable f => (a → Boolean) → f a → f a
filter(fn, list) {
return R.filter(fn, list)
},
// findIndex :: (a → Boolean) → [a] → Number
findIndex(field, val, list) {
return R.findIndex(R.propEq(field, val))(list)
},
// concat :: [a] → [a] → [a]
concat(listA, listB) {
return R.concat(listA, listB)
},
//flatten :: [a] → [b]
flatten(list) {
return R.flatten(list)
},
//// [{}] ////
// getFullObjByField :: k -> [a] -> [a] -> {k:v}
getFullObjByField(findField, partArr, fullArr) {
return R.filter(R.where({ [findField]: R.contains(R.__, partArr) }))(fullArr) // tslint:disable-line
},
示例3: cleanOperators
export function cleanOperators(tokens: IToken[]) {
const output: IToken[] = [];
let temp: IToken[] = [];
let bracketsCount = 0;
tokens.forEach((token) => {
const { image } = token;
if (bracketsCount === 0 && image !== '(' && image !== ')') {
output.push(token);
return;
}
temp.push(token);
if (image === '(') {
bracketsCount += 1;
} else if (image === ')') {
bracketsCount -= 1;
if (bracketsCount === 0) {
// recursive clean within parenthesis, unnests one layer
const cleaned = cleanOperators(temp.slice(1, -1));
if (cleaned.length) {
// Length check means this is fine
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
output.push(R.head(temp)!, ...cleaned, R.last(temp)!);
}
temp = [];
}
}
});
const findFirstRelevant = R.findIndex(
(token: IToken) => MODULE_REGEX.test(token.image) || token.image === '(',
);
const findLastRelevant = R.findLastIndex(
(token: IToken) => MODULE_REGEX.test(token.image) || token.image === ')',
);
const processedTokens = output.slice(findFirstRelevant(output), findLastRelevant(output) + 1);
const removedDuplicates = processedTokens.filter((item, pos, arr) => {
// always keep the first and last element
if (pos === 0 || pos === arr.length - 1) return true;
// then check if each element is different than the one before it
return !(AND_OR_REGEX.test(item.image) && AND_OR_REGEX.test(arr[pos + 1].image));
});
const moduleTokens = [];
// Falsy value if array does not contain unique conjunction
// Need token to inject later on when it is missing between two modules
const singularConjunction = removedDuplicates.reduce(
(acc: IToken | null | false, token: IToken) => {
const { image } = token;
if (image === 'and' || image === 'or') {
if (!acc) return token;
if (acc.image !== image) return false;
}
return acc;
},
null,
);
// Fill in missing values with conjunction found
for (let i = 0; i < removedDuplicates.length; i++) {
const token = removedDuplicates[i];
const nextToken = removedDuplicates[i + 1];
const isModule = MODULE_REGEX.test(token.image);
const isAnotherModule = nextToken && MODULE_REGEX.test(nextToken.image);
moduleTokens.push(token);
if (singularConjunction && isModule && isAnotherModule) {
moduleTokens.push(singularConjunction);
}
}
return moduleTokens;
}