-
Notifications
You must be signed in to change notification settings - Fork 0
Utility Object
Module: sldt/utilsObject
Each function is imported as an import member (there is no default export)
import { isNumber } from "sldt/utilsObject";
Returns true if value is not null
and not undefined
function isDefined(value: any): boolean
A shortcut for typeof value === "number"
, additionally checks if it is finite number. Returns true
if var is a number.
function isNumber(probableNumber: any): boolean
In fact, returns typescript's value is number
, such notation helps in if clauses, because embedded IDE tools will recognize it and hint you with string methods.
if (isNumber(response)) {
console.log(response. /// here you'll see the number methods like split and replace
}
A shortcut for typeof value === "function"
. Returns true
if var is a number. Useful if you accept a callback from somewhere and need to check that you received a function.
function isFunction(probableFunction: any): boolean
In fact, returns typescript's value is function
, such notation helps in if clauses, because embedded IDE tools will recognize it and hint you with string methods.
@api visibleValueTransform;
get showValue() {
if (isFunction(this.visibleValueTransform)) {
return this.visibleValueTransform(this.value);
}
return this.value
}
function isObject(probableObject: any): boolean
As well, returns probableString is object
to help IDE type system recognize an object.
function objectFilterProperties<T>(
source: {[key: string]: T},
filterer: (string[]|(value: T, key: string) => boolean)
): boolean
(key may also be a number or symbol)
filterer
parameter can be either a function that works the same way as Array.filter, or a list of allowed properties
import {isArray} from "sldt/utilsArray"; // add one other sldt lib :)
// cut oppIds=>OLI to object with opportunity id which have an array
function getOpportunityIdsWithLines(/**@type{{[oppId: string]: OLI[]}}*/linesByOpportunityId) {
return objectFilterProperties(linesByOpportunityId, (olis) => isArray(olis))
}
import {isArray} from "sldt/utilsArray"; // add one other sldt lib :)
// publish change on only part of the state
class OpportunityProductPicker {
state = {
opportunity: {},
opportunityLineItems: [],
pricebook: {}
productsAvailable: []
};
fireOpportunityAndLinesUpdate() {
this.dispatchEvent(new CustomEvent('oppupdate', {
detail: objectFilterProperties(this.state, ['opportunity', 'opportunityLineItems']);
}));
}
}
function objectMapProperties<T,V>(
source: {[key:string]: T},
mapper: (value: T, key: string, sourceObject: {[key:string]: T}) => {key?: string, value: V}
): {[key:string]: V}
source
is a source object to transform;
mapper
is a function very likely to an array mapper, but it must return an object with value
and optional key
properties. This is done in order to adjust both key and value at the same time.
// in a grouped Opportunity Line Item list, remove the lines with 0 amount
function filterOnlyValidOLI(olisByOpportunityId) {
return objectMapProperties(olisByOpportunityId, (olis) => ({
value: olis.filter(oli => (isNumber(oli?.UnitPrice) && oli.UnitPrice > 0))
}));
}