Skip to content

Instantly share code, notes, and snippets.

View r3dm1ke's full-sized avatar
🎯
Before software can be reusable it first has to be usable.

Michael Krasnov r3dm1ke

🎯
Before software can be reusable it first has to be usable.
View GitHub Profile
@r3dm1ke
r3dm1ke / control-flow.dart
Created November 25, 2019 20:29
Control flow statements in Dart
import 'dart:io';
void main() {
// Read some text from console
var password = stdin.readLineSync();
// Check if password is correct
if (password == 'abc') {
print('Access granted');
} else if (password == 'xyz') {
@r3dm1ke
r3dm1ke / proxy-default-value.js
Last active February 20, 2023 07:26
Using the Proxy API to set a default value for undefined object properties
const defaultValueHandler = {
get: (obj, property) =>
property in obj ? obj[property] : 'general kenobi'
}
const objectWithDefaultValue = new Proxy({}, defaultValueHandler);
objectWithDefaultValue.a = 'b';
console.log(objectWithDefaultValue.a); // b
@r3dm1ke
r3dm1ke / deferred-imports.dart
Created November 25, 2019 20:41
Deferred imports in Dart
// Importing standart math library
// Note that with deferred imports we have to specify its name
// using _is_ keyword
import 'dart:math' deferred as math;
void main() {
greet();
}
// This is a function that is async. I will talk about
@r3dm1ke
r3dm1ke / setupJest.js
Last active December 5, 2022 14:38
toBeISODate Jest matcher
expect.extend({
toBeISODate(received) {
// This regexp checks for formatting
if (
!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(received)
) {
return {
pass: false,
message: `Expected ${received} to be a valid ISO date string`,
};
@r3dm1ke
r3dm1ke / client.js
Last active January 21, 2022 12:58
Builder pattern in JavaScript
function executeWithFetch(request) {
// EXERCISE FOR THE READER
}
function executeWithAxios(request) {
// EXERCISE FOR THE READER
}
function ClientBuilder() {
return {
expect.extend({
toBePowerOf(received, power) {
if (typeof power !== 'number') {
throw new Error('expected power to be a number');
}
if (typeof received !== 'number') {
throw new Error('expected value to be a number');
}
@r3dm1ke
r3dm1ke / sample-matchers.js
Created February 18, 2020 19:20
Sample matches in Jest
expect(1).toBe(1); // Checking for value and reference equality
expect({a: 'b'}).toEqual({a: 'b'}); //Checking for deep value equality
expect('abacaba').toMatch(/bac/); // Checking if a string matches a regexp
expect({a: 'b', b: 'c'}).toMatchObject({a: 'b'}); // Checking for a partial object match
expect([1, 2, 3]).toContainEqual(2); // Checking if an array contains an element
expect(2).not.toEqual(3); // using not to negate any matcher
expect({a: 'b'}).toMatchObject({
a: expect.any(String)
}); // Type checking
@r3dm1ke
r3dm1ke / Car.js
Created January 12, 2020 17:23
Car class in ES6
class Car {
constructor(make, model) {
this.make = make;
this.model = model;
}
start() {
console.log('vroom');
}
@r3dm1ke
r3dm1ke / Car.js
Created January 10, 2020 16:12
Car class in ES5
// "class" declaration
function Car(make, model) {
this.make = make;
this.model = model;
}
// the start method
Car.prototype.start = function() {
console.log('vroom');
}
const client = ClientBuilder()
.forBaseUrl('https://some-api.com/api/')
.withHeaders({Authorization: 'Bearer ABACABA'})
.usingAxios()
.build();
client.post('UpdateData', {data: 'new data'});