Skip to content

Instantly share code, notes, and snippets.

@hebus
hebus / FakeInputTest.js
Created May 30, 2017 13:50
Two way binding input test
it('should failed to save when slide name is empty', fakeAsync(() => {
let notify: NotifyServiceMock = TestBed.get(NotifyService);
spyOn(notify, 'Warning');
comp.openModal();
fixture.detectChanges();
tick();
// fake favorite name input (two way binding update)
let input = fixture.debugElement.query(By.css('#name'));
@hebus
hebus / fakeAsync.js
Created May 18, 2017 07:48
Simulate keypress on HTMLInput element
it('should search a element', fakeAsync(() => {
comp.openModal();
fixture.detectChanges();
tick();
let DOM = fixture.debugElement.nativeElement;
let presentations = DOM.querySelectorAll('#presentationList li');
expect(presentations.length).toEqual(3);
expect(comp.selectedItem.description).toEqual('description1');
@hebus
hebus / SimpleTextFilter.js
Created May 16, 2017 14:12
Simple text filter
function transform(allValues: any[], value: string) {
if(!allValues) return [];
if (!value || value === '') return allValues;
let pattern = value.replace(/[?=]/g, '\\?').replace(/[!=]/g, '\\!').replace(/[.=]/g, '\\.');
let regX = new RegExp('(?=.*' + pattern.trim().split(' ').join(')(?=.*') + ').*', 'gi');
return allValues.filter(item => ('' + item).match(regX));
}
@hebus
hebus / InsertOrUpdate.js
Last active June 5, 2018 15:59
Add a new item in the collection when not found
let arr = [{code: '123',label: 'A'}, {code: '234',label: 'AB'}, {id: '345',label: 'AB C'}];
let r = transform(arr, 'code', 234);
console.log(r);
function transform(items: any[], field: string, value ? : string): any[] {
if (!items) return [];
let val = (value)?(''+value).trim():value;
if (val) {
console.log ('ici');
@hebus
hebus / FieldFilterPipe.js
Created May 9, 2017 08:09
Angular2 filter pipe by field/value
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'fieldFilter'
})
export class FieldPipe implements PipeTransform {
transform(items: any[], field: string, value ? : any): any[] {
if (!items) return [];
if (value) {
if (value === '') return items;
@hebus
hebus / array_filter.js
Last active May 5, 2017 08:54
Filter an array using regular expression
let arr = [1, 11, 2, 22, 23, 25, 4, 40];
let args = "2";
let pattern = args.replace(/[?=]/g, '\\?').replace(/[!=]/g, '\\!').replace(/[.=]/g, '\\.');
console.log(pattern);
// global insensitive search
// AND
let regX = new RegExp('(?=.*' + pattern.trim().split(' ').join(')(?=.*') + ').*', 'gi');
// OR
//let regX = new RegExp('(?=.*' + pattern.trim().split(' ').join(')|(?=.*') + ').*', 'gi');
console.log(regX);
@hebus
hebus / .js
Created May 3, 2017 09:22
Function to join each element of an array
const f = (arr: any) => Object.keys(arr).map((key) => arr[key]).map((item) => Object.keys(item).map((i) => item[i]).join('_')).map((item) => 'P_' + item);
@hebus
hebus / KeyValue.js
Created May 3, 2017 06:45
Key Value array manipulation in javascript
let arr = {};
arr['key1'] = {id:1, value:'value1'};
arr['key2'] = {id:2, value:'value2'};
console.log(arr);
let keys = Object.keys(arr);
console.log(keys);
let i = Object.keys(arr['key1']);
console.log(i);
@hebus
hebus / concatenate.js
Created May 2, 2017 14:27
Arrays content concatenate
var arr = [{id:1, value:'value1'},{id:2, value:'value2'}];
var arr2 =[{id:3, value:'test'},{id:4, toto:'toto'}];
var a = arr.map((it)=> Object.keys(it).map((i)=> it[i]).join('_')).map((it) => 'P_'+it).join(',');
console.log(a);
a+= ',' + arr2.map((it)=> Object.keys(it).map((i)=> it[i]).join('_')).map((it) => 'T_'+it).join(',');
console.log(a);
@hebus
hebus / Angular2_lostfocus.js
Created April 4, 2017 14:24
Jasmine : Angular2 input lost focus unit test
it('should close the dropdown list when focus is lost', () => {
let fixture = TestBed.createComponent(the_component);
const instance = fixture.componentInstance;
fixture.detectChanges();
let elt = fixture.debugElement.query(By.css('input'));
elt.triggerEventHandler('focus', null);
elt.triggerEventHandler('blur', null);