Skip to content

Instantly share code, notes, and snippets.

View dupski's full-sized avatar

Russell Briggs dupski

  • United Kingdom
View GitHub Profile
@dupski
dupski / ts-property-decorator.ts
Last active January 25, 2017 09:44
Typescript Property Decorator factory test
function format(name: string)
{
return function(target: Object, propertyKey: string | symbol) {
console.log('name:', name, 'factory args', arguments);
};
}
class Greeter {
@format('test')
@dupski
dupski / class.ts
Last active January 28, 2017 01:58
EnterpriseJs - Part 2 - TypeScript Class
class Person {
name: string;
age: number;
constructor(name: string) {
this.name = name;
}
greet() {
console.log(‘Hello, ‘ + this.name);
}
}
@dupski
dupski / people.ts
Created February 28, 2017 06:28
Example of an ES6 Module
class Person {
name: string;
age: number;
}
export class Customer extends Person {
favouriteColour: string;
}
export class Employee extends Person {
@dupski
dupski / import_by_name.ts
Last active February 28, 2017 06:39
Example of importing specific exports from an ES6 module
// Import just the 'Customer' and 'MAXIMUM_PEOPLE' exports from people.ts
import { Customer, MAXIMUM_PEOPLE } from './models/people';
let customer = new Customer('Bob', 34);
console.log(MAXIMUM_PEOPLE);
@dupski
dupski / product.ts
Created February 28, 2017 07:48
Example of an ES6 Module with a default export
export default class Product {
name: string;
price: number;
}
@dupski
dupski / import_default.ts
Last active February 28, 2017 07:57
Example of importing the default export of an ES6 Module
import Prod from './models/product';
let widget = new Prod();
widget.name = 'Blue Widget';
widget.price = 2.88;
import * as request from 'request';
request({ url: 'https://randomuser.me/api/', json: true },
function(error, response, data) {
if (error) {
console.error(error);
}
else {
console.log('Got user data:', data);
}
import * as request from 'request';
request({ url: 'https://randomuser.me/api/', json: true },
function(error, response, data) {
if (error) {
console.error(error);
}
else {
let user = data.results[0];
request({ url: 'http://quotes.rest/qod.json', json: true },
import request from 'axios';
let user: any;
let quote: any;
request({ url: 'https://randomuser.me/api/', responseType: 'json' })
.then((response) => {
user = response.data.results[0];
return request({ url: 'http://quotes.rest/qod.json', responseType: 'json' });
})
import request from 'axios';
Promise.all([
request({ url: 'https://randomuser.me/api/', responseType: 'json' }),
request({ url: 'http://quotes.rest/qod.json', responseType: 'json' })
])
.then((responses) => {
let user = responses[0].data.results[0];
let quote = responses[1].data.contents.quotes[0];
console.log(user.name.first + ' says: ' + quote.quote);