本文整理汇总了TypeScript中ramda.curry函数的典型用法代码示例。如果您正苦于以下问题:TypeScript curry函数的具体用法?TypeScript curry怎么用?TypeScript curry使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了curry函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: return
export function promisify<T>(f: Function): (...args: any[]) => Promise<T> {
return (...args: any[]) =>
new Promise<T>((resolve, reject) => f(...args, (err: any, data: T) => {
if (err) {
reject(err);
} else {
resolve(data);
}
}));
}
const log = R.curry(
(msg?: string, data?: any) => {
if (msg) {
console.log(msg, data);
} else {
console.log(data);
}
return data;
}
);
const glob = R.curry(
(pattern: string, path: string) =>
promisify<string[]>(_glob)(pattern, { root: path })
);
const mv = promisify<void>(_mv);
const inName = R.curry((patterns: string[], file: string) => {
const { name } = path.parse(file);
return R.contains(name, patterns);
示例2: domify
import * as R from 'ramda'
import domify from '../domify/domify'
const splitBySelector = (
{
selector,
onlyChildren = false
}: { selector: string; onlyChildren?: boolean },
markup: string
) => {
const rootNode = domify(markup)
const markups = onlyChildren
? Array.from(rootNode.children)
.filter(node => node.matches(selector))
.map(node => node.innerHTML)
: Array.from(rootNode.querySelectorAll(selector)).map(
node => node.innerHTML
)
return markups
}
export default R.curry(splitBySelector)
示例3: switch
entityID: string | number
) => {
if (!entityType || !entityID) {
return;
}
const _store = store.getState();
const {
linodes,
domains,
nodeBalancers,
images,
volumes
} = _store.__resources;
switch (entityType) {
case 'linode':
return linodes.entities.find(linode => entityID === linode.id);
case 'image':
return images.entities.find(image => entityID === image.id);
case 'nodebalancer':
return nodeBalancers.itemsById[entityID];
case 'domain':
return domains.entities.find(domain => entityID === domain.id);
case 'volume':
return volumes.itemsById[entityID];
default:
return;
}
};
export const getEntityByIDFromStore = curry(_getEntityByIDFromStore);
示例4: urlQueryParser
.forEach((key) => {
query.attributes[key] = raw[key];
});
Object.keys(raw).filter((key) => {
return !R.isNil(model.options.scopes[key]);
})
.forEach((key) => {
query.scope = key;
});
let pagination = ['limit', 'offset', 'order'];
pagination.filter((key) => {
// throw raw[key];
return !R.isNil(raw[key]);
})
.forEach((key) => {
if (key === 'order' && !Array.isArray(raw[key])) {
raw[key] = [raw[key]];
}
query.pagination[key] = raw[key];
});
return query;
};
export default R.curry(function(relationSchema, model, query) {
return urlQueryParser(relationSchema, model, query);
});
示例5: curry
import Rx from 'rxjs';
import {
curry
} from 'ramda';
const listen = curry((event: string, element: HTMLElement) => {
return Rx.Observable.fromEvent(element, event);
});
const subscribe = curry((success, error, stream) => {
stream.subscribe(success, error);
});
export {
listen,
subscribe
};
示例6: merge
//.........这里部分代码省略.........
await transform(axiosRequestConfig)
}
}
}
// after the call, convert the axios response, then execute our monitors
const chain = pipe(
convertResponse(toNumber(new Date())),
// partial(convertResponse, [toNumber(new Date())]),
runMonitors,
)
return instance
.request(axiosRequestConfig)
.then(chain)
.catch(chain)
}
/**
* Fires after we convert from axios' response into our response. Exceptions
* raised for each monitor will be ignored.
*/
const runMonitors = ourResponse => {
monitors.forEach(monitor => {
try {
monitor(ourResponse)
} catch (error) {
// all monitor complaints will be ignored
}
})
return ourResponse
}
/**
* Converts an axios response/error into our response.
*/
const convertResponse = curry((startedAt: number, axiosResult: AxiosResponse | AxiosError) => {
const end: number = toNumber(new Date())
const duration: number = end - startedAt
// new in Axios 0.13 -- some data could be buried 1 level now
const isError = axiosResult instanceof Error || axios.isCancel(axiosResult)
const axiosResponse = axiosResult as AxiosResponse
const axiosError = axiosResult as AxiosError
const response = isError ? axiosError.response : axiosResponse
const status = (response && response.status) || null
const problem = isError ? getProblemFromError(axiosResult) : getProblemFromStatus(status)
const originalError = isError ? axiosError : null
const ok = in200s(status)
const config = axiosResult.config || null
const headers = (response && response.headers) || null
let data = (response && response.data) || null
// give an opportunity for anything to the response transforms to change stuff along the way
let transformedResponse = {
duration,
problem,
originalError,
ok,
status,
headers,
config,
data,
}
if (responseTransforms.length > 0) {
forEach(transform => transform(transformedResponse), responseTransforms)
}
return transformedResponse
})
// create the base object
const sauce = {
axiosInstance: instance,
monitors,
addMonitor,
requestTransforms,
asyncRequestTransforms,
responseTransforms,
addRequestTransform,
addAsyncRequestTransform,
addResponseTransform,
setHeader,
setHeaders,
deleteHeader,
headers,
setBaseURL,
getBaseURL,
get: partial(doRequestWithoutBody, ['get']),
delete: partial(doRequestWithoutBody, ['delete']),
head: partial(doRequestWithoutBody, ['head']),
post: partial(doRequestWithBody, ['post']),
put: partial(doRequestWithBody, ['put']),
patch: partial(doRequestWithBody, ['patch']),
link: partial(doRequestWithoutBody, ['link']),
unlink: partial(doRequestWithoutBody, ['unlink']),
}
// send back the sauce
return sauce
}
示例7: curry
import { compose, curry, defaultTo, isEmpty, not, when } from 'ramda';
export default curry((defaultValue: number, v?: null | string | number) =>
compose<
string | number | null | undefined,
number | null | undefined,
number>(
defaultTo(defaultValue),
when(compose(not, isEmpty), (value: string) => +value),
)(v));
示例8: normalize
}
const replaceBrackets = (str: string) => str.replace(/[{[<]/g, '(').replace(/[}\]>]/g, ')');
const replaceOperators = (str: string) =>
str
.replace(/[|/]/g, OPERATORS.or)
.replace(/[;&]/g, OPERATORS.and)
.replace(/ plus /g, OPERATORS.and)
.replace(OPERATORS_REGEX, R.toLower);
export const normalize = R.pipe(
removeSpaceFromModule,
fixOperatorTypos,
insertPostFixAsStandalone,
convertCommas,
R.curry(convertToNumerals)(1),
replaceBrackets,
fixBrackets,
replaceOperators,
removeModuleTitles,
);
export default function normalizeString(string: string, moduleCode: ModuleCode): string {
// remove own module code from string (e.g. `CS1000R` would remove `CS1000R`, `CS1000`)
// @ts-ignore Strings are lists, damn it
const moduleWithoutPostfix = moduleCode.slice(0, R.findLastIndex(R.test(/\d/), moduleCode) + 1);
const moduleRegex = new RegExp(`\\b${moduleWithoutPostfix}(?:[A-Z]|[A-Z]R)?\\b`, 'g');
const preprocessed = string.replace(moduleRegex, '');
return normalize(preprocessed);
}
示例9:
import _ from 'ramda';
export default _.curry((selector: string, html: string) => {
return document.querySelector(selector).innerHTML = html;
});
示例10: while
import { curryN, curry } from 'ramda';
export const range = (from: number, to: number) => {
const result = [];
let n = from - 1;
while (++n < to) result.push(n);
return result;
};
export const clamp = curry((min: number, max: number, value: number) => {
return Math.max(min, Math.min(max, value));
});
export const clampLoop = curry((min: number, max: number, value: number) => {
const len = Math.abs(max - min);
let a = value % len;
if (a > max) a -= len;
else if (a < min) a += len;
return a;
});
export const absDiff = (a: number, b: number) => Math.abs(b - a);
export const normalize = curry((a: number, b: number, x: number) => (x - Math.min(a, b)) / absDiff(a, b));
export const mapToRange = curry((a: number, b: number, c: number, d: number, x: number) => {
const r = (d - c) / (b - a);
return (x - a) * r + c;
});