Skip to content

Instantly share code, notes, and snippets.

@DejanBelic
DejanBelic / customValidator.ts
Created April 21, 2020 18:04
Angular custom validator - restricted words
private restrictedWords(words) {
return (control: FormControl): {[key: string]: any} => {
if (!words ) { return null; }
const invalidWords = words
.map(word => control.value.includes(word) ? word : null)
.filter(word => word != null);
return invalidWords && invalidWords.length > 0
? { restrictedWords: invalidWords.join(', ')}
: null;
};
@DejanBelic
DejanBelic / template.yml
Created December 11, 2019 08:45
Cognito cloudformation template
AWSTemplateFormatVersion: '2010-09-09'
Resources:
CognitoUserPool:
Type: "AWS::Cognito::UserPool"
Description: "A Cognito user pool for authenticating users"
Properties:
UserPoolName: "NgAppUserPool"
UsernameAttributes: [email, phone_number] # Allow both email or phone number to user can sign in / sign up
Schema:
@DejanBelic
DejanBelic / getClosestElement.js
Created August 29, 2019 08:12
Get closest element with class name vanillajs
function closestByClass(el, className) {
while(!el.classList.contains(className)) {
el = el.parentNode;
if (!el) {
return null;
}
}
return el;
}
@DejanBelic
DejanBelic / filters.js
Created March 29, 2019 08:50
Chaining filters - when having multiple filters to apply
const f1 = h => h.title.toLowerCase().includes(title.toLowerCase());
const f2 = d => d.field_category.toLowerCase().includes(category.toLowerCase());
const f3 = e => e.uid.toLowerCase().includes(author.toLowerCase());
const filtered = nodes.filter(f1).filter(f2).filter(f3);
@DejanBelic
DejanBelic / isEmpty.js
Created March 2, 2019 09:41
Is empty function javascript
const isEmpty = value =>
value === undefined ||
value === null ||
(typeof value === "object" && Object.keys(value).length === 0) ||
(typeof value === "string" && value.trim().length === 0);
@DejanBelic
DejanBelic / reduxStore.js
Last active February 3, 2019 11:34
Redux store
function createStore(reducer) {
// Store should have 4 parts.
// 1. The state.
// 2. Get the state.
// 3. Listen to state change.
// 4. Update the state.
let state;
let listeners = [];
function htmlEscape(str) {
return str.replace(/>/g,'&')
.replace(/</g,'&gt;')
.replace(/"/g,'&lt;')
.replace(/'/g,'&quot;')
.replace(/`/g,'&#39;')
.replace(/`/g,'&#96;');
}
import React, { Component } from 'react';
import './App.css';
import { AuthProvider } from './Hoc/AuthProvider/AuthProvider';
import Login from './Containers/Login/Login';
import ProtectedRoute from './Components/ProtectedRoute/ProtectedRoute';
class App extends Component {
render() {
return (
@DejanBelic
DejanBelic / moment.js
Last active June 7, 2018 12:38
MomentJS get today date.
// MomentJS library.
// Filter array and get only items that are from today (date).
var getTodayEvents = someArray.filter(todayDate => moment(todayDate.start.format("YYYY-MM-DD")).isSame(moment().format("YYYY-MM-DD")))
@DejanBelic
DejanBelic / async await ie11.js
Last active May 11, 2023 15:09
How to use async await in ie11
// Async await func
async function getTimelineData() {
var response = await fetch('/some-api-url')
return response.json()
}
async function populateTimelineInit() {
var data = await getTimelineData();
new vis.DataSet(data);