Skip to content

Instantly share code, notes, and snippets.

View robertdempsey's full-sized avatar

Robert Dempsey robertdempsey

View GitHub Profile
@robertdempsey
robertdempsey / ChangeDetectorRef.ts
Created May 7, 2020 10:56
Triggers change detection with OnPush strategy even if it is manually triggered from outside.
import { Component, EventEmitter, Input, Output, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
@Component({
selector: 'display',
templateUrl: './display.component.html',
styleUrls: ['./display.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class DisplayComponent {
constructor(private changeDetectorRef: ChangeDetectorRef) {
const displayRoute: RouteData<DisplayComponent> = {
path: 'RouteComponentExample/:route_id',
component: RouteComponent,
data: {
component: {
type: DisplayComponent,
inputs: {
inputData: `This is populated with static data from our 'inputs' property in route data`
},
outputs: {
const displayRoute: RouteData<DisplayComponent> = {
path: 'RouteComponentExample/:route_id',
component: RouteComponent,
data: {
component: {
type: DisplayComponent,
inputs: {
inputData: `This is populated with static data from our 'inputs' property in route data`
},
outputs: {
@robertdempsey
robertdempsey / ES6RestAndDeleteOmit.ts
Created April 18, 2020 11:36
Supports omitting multiple properties, at the top level only
const omit = (originalObject = {}, keysToOmit = []) => {
const clonedObject = { ...originalObject };
for (const path of keysToOmit) {
delete clonedObject[path]
}
return clonedObject;
}
@robertdempsey
robertdempsey / LodashCloneDeepAndUnset.ts
Created April 18, 2020 10:53
Allows flattened paths to be omitted and does not modify our original object
const omit = (originalObject = {}, keysToOmit = []) => {
const clonedObject = _.cloneDeep(originalObject);
for (const path of keysToOmit) {
_.unset(clonedObject, path)
}
return clonedObject;
}
const omit = (originalObject = {}, keysToOmit = []) => {
const clonedObject = { ...originalObject };
for (const path of keysToOmit) {
_.unset(clonedObject, path)
}
return clonedObject;
}
@robertdempsey
robertdempsey / LodashPickByToOmit.ts
Last active April 8, 2020 11:21
Uses Lodash 'pickBy' to omit properties whose keys do not match those we've specified.
const omit = (originalObject: object, keys: string[]) => _.pickBy(objectFromFrontend, (value, key) => !keys.includes(key))
const objectFromFrontend = {
_id: 5,
data: {
some: 1,
useful: 2,
data: 3
}
}
@robertdempsey
robertdempsey / PickAsAlternativeToOmit.ts
Last active April 7, 2020 11:30
Uses 'pick' to explicitly select the properties we want to keep, as opposed to throwing away the ones we don't.
const objectFromFrontend = {
_id: 5,
data: {
some: 1,
useful: 2,
data: 3
}
}
const objectToInsertIntoDB = _.pick(objectFromFrontend, ['data.useful', 'data.data']);
@robertdempsey
robertdempsey / ES2019OmitMultiProperty.ts
Last active April 11, 2020 11:04
A native omit function that allows you to specify multiple properties to omit.
const omit = (keysToOmit: string[], originalObj = {}) =>
Object.fromEntries(
Object.entries(originalObj)
.filter(([key]) => !keysToOmit.includes(key))
)
const objectFromFrontend = {
_id: 5,
data: {
some: 1,
@robertdempsey
robertdempsey / ES6OmitSingleProperty.ts
Last active April 8, 2020 10:55
An alternative to Lodash omit, which allows you to omit a single property.
const omit = (keyToOmit: string, { [keyToOmit]: _, ...omittedPropObj } = {}) => omittedPropObj;
const objectFromFrontend = {
_id: 5,
data: {
some: 1,
useful: 2,
data: 3
}
}