(by @andrestaltz)
If you prefer to watch video tutorials with live-coding, then check out this series I recorded with the same contents as in this article: Egghead.io - Introduction to Reactive Programming.
import * as R from 'ramda' | |
const appendCountryCode = R.curry((number, code) => `${code} ${number}`); |
it('removes the zm country code', () => { | |
expect(removePrefix("260923423")).toBe("923423") | |
}) | |
it('removes the starting zero', () => { | |
expect(removePrefix("0923423")).toBe("923423") | |
}) | |
// in the above tests you can see that the end result is all the same. |
// zambian mobile numbers start with 09x in case the user typed it start with a 2 | |
// then its a country code prefixed | |
// es6 | |
const removePrefix = num => num[0] == 2 ? num.substring(3) : num[0] == 0 ? num.substring(1) : num; | |
// es5 | |
function removePrefix(num){ | |
if(num[0] == 2){ // check if it starts with a 2 | |
return num.substring(3) // remove numbers up to index 2 of the value |
import { removeSymbol } from '../utils' | |
it("should remove the + symbol if it exists", () => { | |
expect(removeSymbol("+260")).toBe("260") | |
}) | |
it("should return the given value if not + symbol found", () => { | |
expect(removeSymbol("500")).toBe("500") | |
}) | |
// since the argument should be a string, let's test that as well |
function removeSymbol(number){ | |
// make sure we are getting a string | |
if (typeof number !== "string") throw new TypeError("Expected string"); | |
// check if the number has the + sign and remove it | |
if(number.includes("+")){ | |
return number.replace(/\+/gi, "") | |
} | |
return number | |
} |
function add(a, b){ | |
return a + b | |
} | |
// test if the add really adds two numbers | |
it('should add two given numbers', () => { | |
expect(add(2, 5)).toBe(7) | |
}) |
import React, { useState } from "react"; | |
const removeSymbol = num => (num.includes("+") ? num.replace(/\+/gi, "") : num); | |
const hasPrefix = num => num[0] == 2 ? num.substring(3) : num[0] == 0 ? num.substring(1) : num; | |
function App() { | |
const [number, setNumber] = useState(""); | |
function parseNumber({ target: { value } }) { | |
setNumber(value); | |
} |
function parseText(text) { | |
let filterSpace = item => item.length > 1; | |
let spacedWords = text | |
.split(" ") | |
.filter(filterSpace) | |
.map(word => `${word} `); | |
spacedWords.map((word, i) => { | |
if (!word.indexOf("_")) { | |
let newWord = <span style={{ color: "blue" }}>{word.substring(1)}</span>; |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect/1.20.2/expect.js"></script> | |
</head> | |
<body> |
(by @andrestaltz)
If you prefer to watch video tutorials with live-coding, then check out this series I recorded with the same contents as in this article: Egghead.io - Introduction to Reactive Programming.