Skip to content

Instantly share code, notes, and snippets.

View marcelmokos's full-sized avatar

Marcel Mokoš marcelmokos

View GitHub Profile
import { getChangesOnList } from './getChangesOnList';
describe('getChangesOnList', () => {
type TestItem = { id: number; value: string };
const testItemA: TestItem = { id: 1, value: 'A' };
const testItemB: TestItem = { id: 2, value: 'B' };
const testItemC: TestItem = { id: 3, value: 'C' };
const testItems: TestItem[] = [testItemA, testItemB, testItemC];
import { getChangesOnList } from './getChangesOnList';
describe('getChangesOnList', () => {
it('should return the correct changes', () => {
const prev = [
{ id: 1, name: 'foo' },
{ id: 2, name: 'bar' },
];
const curr = [
{ id: 1, name: 'foo' },
import { getChangesOnList } from './getChangesOnList';
describe('getChangesOnList', () => {
const prevList = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Mary' },
{ id: 3, name: 'Bob' },
];
const currList = [
{ id: 1, name: 'John' },
@marcelmokos
marcelmokos / getChangesOnList.ts
Created March 10, 2023 00:18
/** * Computes the changes between two lists of items. * @param prev The previous list of items. * @param curr The current list of items. * @param isEqual The comparison function to use to determine if two items are deep equal. * @returns An object containing three properties: * - add: the items that were added to the current list. * - update: t…
import isEqualFn from 'lodash/fp/isEqual';
type Item = { id: string | number };
/**
* Computes the changes between two lists of items.
* @param prev The previous list of items.
* @param curr The current list of items.
* @param isEqual The comparison function to use to determine if two items are deep equal.
* @returns An object containing three properties:
@marcelmokos
marcelmokos / axiosInstance.ts
Created December 7, 2021 14:57
Axios instance
import axios from "axios";
import {history} from "../helpers/history";
import {
makeAuthorizationHeader,
isTokenValid,
getToken,
setToken,
tokenIsExpired,
} from "../helpers/token";
const article = {
"id": "a2f693f1-e63c-4190-81b3-7a0b459a2517",
"author": {
"id": "6e9418c3-8658-466f-81d6-58ac5548d3dd",
"displayName": "Gene Price",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/iamsteffen/128.jpg",
"color": "#486071",
"url": "author/Gene-Price_6e9418c3-8658-466f-81d6-58ac5548d3dd"
},
"title": "at odit nulla",
@marcelmokos
marcelmokos / propTypes-isRequired.js
Created October 16, 2019 21:08
propTypes isRequired
Component.propTypes = {
requiredArray: PropTypes.array.isRequired,
requiredBool: PropTypes.bool.isRequired,
requiredFunc: PropTypes.func.isRequired,
requiredNumber: PropTypes.number.isRequired,
requiredObject: PropTypes.object.isRequired,
requiredString: PropTypes.string.isRequired,
requiredSymbol: PropTypes.symbol.isRequired,
// ...
};
@marcelmokos
marcelmokos / propTypes.js
Created October 16, 2019 21:06
propTypes
Component.propTypes = {
optionalArray: PropTypes.array,
optionalBool: PropTypes.bool,
optionalFunc: PropTypes.func,
optionalNumber: PropTypes.number,
optionalObject: PropTypes.object,
optionalString: PropTypes.string,
optionalSymbol: PropTypes.symbol,
// ...
};
@marcelmokos
marcelmokos / typed-object-argument-function.ts
Created October 16, 2019 20:55
typed object argument function
const fullName = (
{firstName = "", lastName = ""} // we are providing default values
: {firstName: string, lastName: string} // type, can be inferenced
= {} // default value object for object argument
) => `${firstName} ${lastName}`;
@marcelmokos
marcelmokos / functions.ts
Created October 16, 2019 19:51
typing typescript functions
function add(a: number, b: number): number { return a + b };
const add = (a: number, b: number): number => a + b;
type Arithmetics = (a: number, b: number) => number;
// or
/*
interface Arithmetics {
(a: number, b: number): number;
}