More functions

This commit is contained in:
fkereki
2018-04-01 12:13:22 -03:00
parent 636b0c6db4
commit 2c47a2904c
6 changed files with 132 additions and 18 deletions
+6 -13
View File
@@ -15,10 +15,7 @@
"author": "Federico Kereki", "author": "Federico Kereki",
"license": "ISC", "license": "ISC",
"babel": { "babel": {
"presets": [ "presets": ["env", "flow"]
"env",
"flow"
]
}, },
"eslintConfig": { "eslintConfig": {
"parserOptions": { "parserOptions": {
@@ -31,16 +28,12 @@
"node": true, "node": true,
"es6": true "es6": true
}, },
"extends": [ "extends": ["eslint:recommended", "plugin:flowtype/recommended"],
"eslint:recommended", "plugins": ["babel", "flowtype"],
"plugin:flowtype/recommended"
],
"plugins": [
"babel",
"flowtype"
],
"rules": { "rules": {
"no-console": "off" "no-console": "off",
"no-var": "error",
"prefer-const": "error"
} }
}, },
"prettier": { "prettier": {
+40
View File
@@ -0,0 +1,40 @@
/* @flow */
/* eslint-disable no-unused-vars, one-var */
function Show(value: mixed): void {
this.saved = value;
setTimeout(function() {
console.log(this.saved);
}, 1000);
}
const w = new Show("Doesn't work..."); // instead, "undefined" is shown
function Show1(value: mixed): void {
this.saved = value;
setTimeout(
function() {
console.log(this.saved);
}.bind(this),
1000
);
}
function Show2(value: mixed): void {
this.saved = value;
const that = this;
setTimeout(function() {
console.log(that.saved);
}, 2000);
}
function Show3(value: mixed): void {
this.saved = value;
setTimeout(() => {
console.log(this.saved);
}, 3000);
}
const x = new Show1("This");
const y = new Show2("always");
const z = new Show3("works");
+40
View File
@@ -0,0 +1,40 @@
/* @flow */
/* eslint-disable no-unused-vars */
opaque type dniType: string = string;
type nameType = string; // not opaque!
const stringToDni = (st: string): dniType => {
/*
do validations on st
if OK, return a dniType
if wrong, throw an error
*/
return (st: dniType);
};
export type { dniType, nameType };
export { stringToDni };
class Client {
id: number;
dni: dniType;
name: nameType;
securityToken: string;
constructor(anId: number, aDni: dniType, aName: nameType) {
this.id = anId;
this.dni = aDni;
this.name = aName;
this.securityToken = "generate.some.token.somehow";
}
showNameAndDni(t: string): void {
console.log(`${t} - Name: ${this.name} DNI: ${this.dni}`);
}
useTokenForSomething(): void {
// do something with the token
}
}
+28
View File
@@ -0,0 +1,28 @@
/* @flow */
/* eslint-disable no-unused-vars */
import type { dniType, nameType } from "./opaque_types";
import { stringToDni } from "./opaque_types";
function updateClient(id: number, dni: dniType, name: nameType) {
/*
Talk to some server
Update the dni and name for the client with given id
*/
}
const newDni = "1234567-8"; // supposedly a DNI
const newName = "Kari Nordmann";
updateClient(229, newName, newDni); // doesn't work; 2nd argument should be a dni
updateClient(229, newDni, newName); // doesn't work either; same reason
updateClient(229, stringToDni(newDni), newName); // OK!
/*
Constraints
*/
function showText(st: string) {
console.log(`Important message: ${st}`);
}
const anotherDni: dniType = stringToDni("9876543-2");
showText(anotherDni); // error, if no subtype constraint is added!
+9 -3
View File
@@ -1,5 +1,5 @@
/* @flow */ /* @flow */
/* eslint-disable no-unused-vars */ /* eslint-disable prefer-const,no-unused-vars,no-unused-labels */
let values = [22, 9, 60, 12, 4, 56]; let values = [22, 9, 60, 12, 4, 56];
const maxOfValues = Math.max(...values); // 60 const maxOfValues = Math.max(...values); // 60
@@ -14,11 +14,17 @@ let person = { name: "Juan", age: 24 };
let copyOfPerson = { ...person }; let copyOfPerson = { ...person };
let expandedPerson = { ...person, sister: "María" }; let expandedPerson = { ...person, sister: "María" };
const average = (...nums: Array<number>): number => { function average(...nums: Array<number>): number {
let sum = 0; let sum = 0;
for (let i = 0; i < nums.length; i++) { for (let i = 0; i < nums.length; i++) {
sum += nums[i]; sum += nums[i];
} }
return sum / nums.length; return sum / nums.length;
}; }
console.log(average(22, 9, 60, 12, 4, 56)); // 27.166667 console.log(average(22, 9, 60, 12, 4, 56)); // 27.166667
const simpleAction = (t: string, d: mixed): Object => {
type: t;
data: d;
return {};
};
+9 -2
View File
@@ -24,10 +24,17 @@ numbersList[1] = "SEP"; // error; cannot assign a string to a number
let anotherList: number[]; let anotherList: number[];
let sealedObject: { name: string, age?: number }; let sealedObject: { name: string, age?: number } = { name: "" };
sealedObject.name = "Ivan Horvat"; // OK sealedObject.name = "Ivan Horvat"; // OK
sealedObject.id = 229; // error: key isn't defined in the data type sealedObject.id = 229; // error: key isn't defined in the data type
sealedObject = { age: 57 }; // error: mandatory field is missing sealedObject = { age: 57 }; // error: mandatory "name" field is missing
let unsealedObject = {}; let unsealedObject = {};
unsealedObject.id = 229; // OK unsealedObject.id = 229; // OK
const toString2 = (x: number): string => {
return x + "x";
};
type numberToString = number => string;
const toString3: numberToString = (x: number) => String(x);