This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as R from 'ramda' | |
const formatNumber = R.compose( | |
appendCountryCode, | |
removePrefix, | |
removeSymbol | |
); | |
// the above function can be called like this | |
formatNumber(numberToformat)(countryCode) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as R from 'ramda' | |
const appendCountryCode = R.curry((number, code) => `${code} ${number}`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |