marioosh.net

mini blog / notes

*ngTemplateOutlet

<ng-template #toolbar> lorem ipsum… </ng-template>   <!– put my template here –> <ng-container *ngTemplateOutlet="toolbar"></ng-container>   <!– and here –> <ng-container *ngTemplateOutlet="toolbar"></ng-container>   <!– and here, too 😉 –> <ng-container *ngTemplateOutlet="toolbar"></ng-container><ng-template #toolbar> lorem ipsum… </ng-template> <!– put my template here –> <ng-container *ngTemplateOutlet="toolbar"></ng-container> <!– and here –> <ng-container *ngTemplateOutlet="toolbar"></ng-container> <!– and here, too 😉 –> <ng-container Read more about *ngTemplateOutlet[…]

array to map with reduce()

let months = [ {month: 1, data1: "test1", data2: "test2"}, {month: 2, data1: "test1", data2: "test2"}, {month: 3, data1: "test1", data2: "test2"} ];   let monthsAsMap = months.reduce((map, obj) => { map[obj.month] = obj; return map; }, {});   console.log(monthsAsMap); console.log(monthsAsMap[’2’]);let months = [ {month: 1, data1: "test1", data2: "test2"}, {month: 2, data1: "test1", data2: Read more about array to map with reduce()[…]

Simple AES-128 encryption in Java

import java.io.ByteArrayOutputStream;   import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec;   /** * AES-128 */ public class EncryptionUtils {   /** * initialisation vector */ private final static byte[] IV = "thai8bai9voo1haV".getBytes();   /** * algorithm/mode/padding */ private final static String TRANSFORMATION = "AES/CBC/PKCS5Padding";   /** * * @param text encrypted text * @param password need Read more about Simple AES-128 encryption in Java[…]

AES-128 in js/Node.js

const iv = Buffer.from(’aeghei1Di8tieNg0’);   exports.encrypt = function(text, password){ try { var cipher = crypto.createCipheriv(’aes-128-cbc’,password,iv); var crypted = cipher.update(text,’utf8′,’hex’); crypted += cipher.final(’hex’); return crypted; } catch (err) { console.error(’encrypt error’,err); return null; } };   exports.decrypt = function(text, password){ try { var decipher = crypto.createDecipheriv(’aes-128-cbc’,password,iv); var dec = decipher.update(text,’hex’,’utf8’); dec += decipher.final(’utf8’); return dec; } Read more about AES-128 in js/Node.js[…]

nested form group

this.form = this.fb.group({ ‘name’:[”], ‘nested’: this.fb.group({ ‘date’:[”] // … }) });this.form = this.fb.group({ ‘name’:[”], ‘nested’: this.fb.group({ ‘date’:[”] // … }) }); <form [formGroup]="form" > <input formControlName="name" … /> <div formGroupName="nested"> <input formControlName="date" … /> </div> </form><form [formGroup]="form" > <input formControlName="name" … /> <div formGroupName="nested"> <input formControlName="date" … /> </div> </form>

@PreAuthorize with use Spring Expression Language (SpEL) and service method call

public interface EventRepository extends JpaRepository<Event, Long> {   @PreAuthorize("@securityService.canSave(#event,principal)") Event save(@Param("event") Event event);   @PreAuthorize("@securityService.canSave2(#event)") Event save2(@Param("event") Event event); }   @Service("securityService") public class SecurityService {   public boolean canSave(Event event, UserDetails principal) { // some logic here }   public boolean canSave2(Event event) {   // get principal from Security Context Object principal = Read more about @PreAuthorize with use Spring Expression Language (SpEL) and service method call[…]

AsyncPipe example

@Component({ … }) export class TestingComponent implements OnInit { units: any; constructor(private restService:RestService) { } ngOnInit() { this.units = this.restService.getUnits(); } }@Component({ … }) export class TestingComponent implements OnInit { units: any; constructor(private restService:RestService) { } ngOnInit() { this.units = this.restService.getUnits(); } } Template <ng-template #loading>Loading…</ng-template> <div *ngIf="units|async as units; else loading"> <span *ngFor="let unit Read more about AsyncPipe example[…]

JSON -> List

import org.springframework.core.io.Resource; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper;   …   @Value("classpath:/strings.json") private Resource jsonFile;   @Autowired private ObjectMapper mapper;   …   List<String> listOfStrings = Arrays.asList( mapper.readValue(jsonFile.getInputStream(), String[].class) );import org.springframework.core.io.Resource; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; … @Value("classpath:/strings.json") private Resource jsonFile; @Autowired private ObjectMapper mapper; … List<String> listOfStrings = Arrays.asList( mapper.readValue(jsonFile.getInputStream(), String[].class) );