本文整理汇总了TypeScript中underscore.mapObject函数的典型用法代码示例。如果您正苦于以下问题:TypeScript mapObject函数的具体用法?TypeScript mapObject怎么用?TypeScript mapObject使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了mapObject函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: newCertsToLinks
newCertsToLinks(newCerts:any, updates:any) {
let newLinks:any = {};
_.mapObject(newCerts, function(certs:any, pubkey:string) {
newLinks[pubkey] = _.pluck(certs, 'from');
});
_.mapObject(updates, function(certs:any, pubkey:string) {
newLinks[pubkey] = (newLinks[pubkey] || []).concat(_.pluck(certs, 'pubkey'));
});
return newLinks;
}
示例2: function
static respondToResourceGET<T>(requestBody, res, dao: DAO<T>, whereArray: string[], likeArray: string[]) {
let whereQuery = _.omit(_.pick(requestBody, whereArray),
function (value, key, object) {
return value === "";
});
let likeQuery_ = _.chain(requestBody)
.pick(requestBody, likeArray)
.omit(function (value, key, object) {
return value === "";
}).value();
let likeQuery = _.mapObject(likeQuery_, function (value, key) {
return "%" + value + "%";
});
dao.find({
where: whereQuery, like: likeQuery, orderBy: requestBody.orderBy
}, function (err, results) {
if (err) {
APIHelper.sendDatabaseErrorResponse(res, err);
} else {
if (results.length === 0) {
APIHelper.sendNotFoundResponse(res, "Not found.");
} else {
APIHelper.sendResponse(res, results);
}
}
});
}
示例3: computeNewCerts
async computeNewCerts(forBlock:number, theNewcomers:any, joinData:any) {
const newCerts:any = {}, certifiers = [];
const certsByKey = _.mapObject(joinData, function(val:any){ return val.certs; });
for (const newcomer of theNewcomers) {
// New array of certifiers
newCerts[newcomer] = newCerts[newcomer] || [];
// Check wether each certification of the block is from valid newcomer/member
for (const cert of certsByKey[newcomer]) {
const isAlreadyCertifying = certifiers.indexOf(cert.from) !== -1;
if (!(isAlreadyCertifying && forBlock > 0)) {
if (~theNewcomers.indexOf(cert.from)) {
// Newcomer to newcomer => valid link
newCerts[newcomer].push(cert);
certifiers.push(cert.from);
} else {
let isMember = await this.dal.isMember(cert.from)
// Member to newcomer => valid link
if (isMember) {
newCerts[newcomer].push(cert);
certifiers.push(cert.from);
}
}
}
}
}
return newCerts;
}
示例4: searchLoadout
export function searchLoadout(storeService: StoreServiceType, store: DimStore): Loadout {
let items = storeService.getAllItems().filter((i) => {
return i.visible &&
!i.location.inPostmaster &&
!i.notransfer;
});
items = addUpStackables(items);
const itemsByType = _.mapObject(_.groupBy(items, 'type'), (items) => limitToBucketSize(items, store.isVault));
// Copy the items and mark them equipped and put them in arrays, so they look like a loadout
const finalItems = {};
_.each(itemsByType, (items, type) => {
if (items) {
finalItems[type.toLowerCase()] = items.map((i) => {
const copiedItem = copy(i);
copiedItem.equipped = false;
return copiedItem;
});
}
});
return {
classType: -1,
name: t('Loadouts.FilteredItems'),
items: finalItems
};
}
示例5: optimalLoadout
export function optimalLoadout(applicableItems: DimItem[], bestItemFn: (item: DimItem) => number, name: string): Loadout {
const itemsByType = _.groupBy(applicableItems, 'type');
// Pick the best item
let items = _.mapObject(itemsByType, (items) => _.max(items, bestItemFn));
// Solve for the case where our optimizer decided to equip two exotics
const getLabel = (i) => i.equippingLabel;
// All items that share an equipping label, grouped by label
const overlaps: _.Dictionary<DimItem[]> = _.groupBy(Object.values(items).filter(getLabel), getLabel);
_.each(overlaps, (overlappingItems) => {
if (overlappingItems.length <= 1) {
return;
}
const options: _.Dictionary<DimItem>[] = [];
// For each item, replace all the others overlapping it with the next best thing
for (const item of overlappingItems) {
const option = copy(items);
const otherItems = overlappingItems.filter((i) => i !== item);
let optionValid = true;
for (const otherItem of otherItems) {
// Note: we could look for items that just don't have the *same* equippingLabel but
// that may fail if there are ever mutual-exclusion items beyond exotics.
const nonExotics = itemsByType[otherItem.type].filter((i) => !i.equippingLabel);
if (nonExotics.length) {
option[otherItem.type] = _.max(nonExotics, bestItemFn);
} else {
// this option isn't usable because we couldn't swap this exotic for any non-exotic
optionValid = false;
}
}
if (optionValid) {
options.push(option);
}
}
// Pick the option where the optimizer function adds up to the biggest number, again favoring equipped stuff
if (options.length > 0) {
const bestOption = _.max(options, (opt) => sum(Object.values(opt), bestItemFn));
items = bestOption;
}
});
// Copy the items and mark them equipped and put them in arrays, so they look like a loadout
const finalItems: { [type: string]: DimItem[] } = {};
_.each(items, (item, type) => {
const itemCopy = copy(item);
itemCopy.equipped = true;
finalItems[type.toLowerCase()] = [itemCopy];
});
return {
classType: -1,
name,
items: finalItems
};
}
示例6: filterLoadoutToEquipped
function filterLoadoutToEquipped(loadout: Loadout) {
const filteredLoadout = copy(loadout);
filteredLoadout.items = _.mapObject(filteredLoadout.items, (items) => {
return items.filter((i) => i.equipped);
});
return filteredLoadout;
}
示例7: parseExpressionToType
function parseExpressionToType(expression: AST.ExpressionType) : types.Type {
if (expression.type === 'expressionarrayLiteral') {
return types.createArrayType(expression.value.map(val => parseExpressionToType(val)));
}
else if (expression.type === 'expressionmapLiteral') {
return types.createMapType(_.mapObject(expression.value, v => {
return parseExpressionToType(v);
}));
}
else if (expression.type === 'expressionidentifier') {
return types.createReferenceType(expression.value);
}
console.error("Couldn't parse dat type boi");
return types.getAnyType();
}
示例8: gatherEngramsLoadout
export function gatherEngramsLoadout(
storeService: StoreServiceType,
options: { exotics: boolean } = { exotics: false }
): Loadout {
const engrams = storeService.getAllItems().filter((i) => {
return i.isEngram() && !i.location.inPostmaster && (options.exotics ? true : !i.isExotic);
});
if (engrams.length === 0) {
let engramWarning = t('Loadouts.NoEngrams');
if (options.exotics) {
engramWarning = t('Loadouts.NoExotics');
}
throw new Error(engramWarning);
}
const itemsByType = _.mapObject(_.groupBy(engrams, 'type'), (items) => {
// Sort exotic engrams to the end so they don't crowd out other types
items = _.sortBy(items, (i) => {
return i.isExotic ? 1 : 0;
});
// No more than 9 engrams of a type
return _.first(items, 9);
});
// Copy the items and mark them equipped and put them in arrays, so they look like a loadout
const finalItems = {};
_.each(itemsByType, (items, type) => {
if (items) {
finalItems[type.toLowerCase()] = items.map((i) => {
return copy(i);
});
}
});
return {
classType: -1,
name: t('Loadouts.GatherEngrams'),
items: finalItems
};
}
示例9: booksGET
function booksGET(req: Request, res: Response) {
let wrongKeys = _.omit(req.query, ["title", "author",
"isbn", "pages", "editorid", "year", "orderBy"]);
if (_.keys(wrongKeys).length > 0) {
APIHelper.sendInvalidArgumentsResponse(res, _.keys(wrongKeys));
}
let constraints = {
title: {
presence: false,
},
isbn: {
presence: false
},
author: {
presence: false
},
pages: {
presence: false,
numericality: {
onlyInteger: true,
greaterThan: 0,
}
},
year: {
presence: false,
numericality: {
onlyInteger: true,
}
},
editorid: {
presence: false,
numericality: {
onlyInteger: true,
}
}
};
let requestValid = APIHelper.validateSearchRequest(res, req.query, constraints);
console.log("WTF");
if (requestValid) {
let whereQuery = _.omit(_.pick(req.query, "pages", "year", "editorid"),
function (value, key, object) {
return value === "";
});
let likeQuery_ = _.chain(req.query)
.pick(req.query, "title", "isbn", "author")
.omit(function (value, key, object) {
return value === "";
}).value();
let likeQuery = _.mapObject(likeQuery_, function (value, key) {
return "%" + value + "%";
});
console.log(req.query.orderBy);
Book.dao.find({
where: whereQuery, like: likeQuery, orderBy: req.query.orderBy
}, function (err, books) {
if (err) {
APIHelper.sendDatabaseErrorResponse(res, err);
} else {
let asyncToDo = books.length;
if (asyncToDo === 0) {
APIHelper.sendNotFoundResponse(res, "No books found.");
} else {
_.each(books, function (value, index, list) {
Editor.dao.find({where: {id: list[index].editorid}}, function (err, editors) {
list[index].editor = editors[0];
asyncToDo--;
if (asyncToDo === 0) {
asyncToDo--;
APIHelper.sendResponse(res, books);
}
});
});
}
}
});
}
}
示例10: bookSearchGET
function bookSearchGET(req: Request, res: Response, next: NextFunction) {
if (Object.keys(req.query).length === 0 && req.baseUrl !== "/api") {
res.render("book/search", {data: {}});
}
else {
let constraints = {
title: {
presence: false,
},
isbn: {
presence: false
},
author: {
presence: false
},
pages: {
presence: false,
numericality: {
onlyInteger: true,
greaterThan: 0,
}
},
year: {
presence: false,
numericality: {
onlyInteger: true,
}
},
editorid: {
presence: false,
numericality: {
onlyInteger: true,
}
}
};
let book: Book = new Book();
let validation = validate(req.query, constraints);
let data = {
errors: {global: ""},
info: {confirmation: ""},
books: [],
book: {}
};
let wrongInputs = 0;
for (let prop in validation) {
if (!validation.hasOwnProperty(prop)) {
continue;
}
data.errors[prop] = validation[prop];
delete req.query[prop];
wrongInputs++;
}
for (let prop in req.query) {
if (!req.query.hasOwnProperty(prop)) {
continue;
}
book[prop] = req.query[prop];
}
if (wrongInputs === 0) {
let whereQuery = _.omit(_.pick(req.query, "pages", "year", "editorid"),
function (value, key, object) {
return value === "";
});
let likeQuery_ = _.chain(req.query)
.pick(req.query, "title", "isbn", "author")
.omit(function (value, key, object) {
return value === "";
}).value();
let likeQuery = _.mapObject(likeQuery_, function (value, key) {
return "%" + value + "%";
});
console.log("Like query=");
console.log(likeQuery);
Book.dao.find({
where: whereQuery, like: likeQuery
}, function (err, books) {
if (err) {
data.errors.global = err.code;
} else {
data.books = books;
let asyncToDo = data.books.length;
_.each(data.books, function (value, index, list) {
Editor.dao.find({where: {id: list[index].editorid}}, function (err, editors) {
list[index].editor = editors[0];
asyncToDo--;
if (asyncToDo === 0) {
data.info.confirmation = books.length + " books found.";
data.book = req.query;
if (req.baseUrl === "/api") {
res.json(data);
return;
}
if (req.xhr) {
//.........这里部分代码省略.........