Commit a4c6af53 authored by Hubert SABLONNIERE's avatar Hubert SABLONNIERE

lint and format code according to StandardJS

parent c9743b01
......@@ -4,15 +4,15 @@
const map = require('map-stream')
function capitalize(text) {
function capitalize (text) {
return text[0].toUpperCase() + text.slice(1).toLowerCase()
}
function capitalizeArray(array) {
function capitalizeArray (array) {
return array.map((text) => capitalize(text))
}
function capitalizeKeys(object) {
function capitalizeKeys (object) {
const result = {}
Object.entries(object).forEach(([key, value]) => {
result[capitalize(key)] = value
......@@ -20,7 +20,7 @@ function capitalizeKeys(object) {
return result
}
function asyncCapitalize(text) {
function asyncCapitalize (text) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (text == null) {
......@@ -31,7 +31,7 @@ function asyncCapitalize(text) {
})
}
function capitalizeStream() {
function capitalizeStream () {
return map((text, next) => {
if (typeof text !== 'string') {
return next(new TypeError('cannot capitalized a non String'))
......@@ -40,4 +40,10 @@ function capitalizeStream() {
})
}
module.exports = { capitalize, capitalizeArray, capitalizeKeys, asyncCapitalize, capitalizeStream }
module.exports = {
capitalize,
capitalizeArray,
capitalizeKeys,
asyncCapitalize,
capitalizeStream
}
......@@ -7,14 +7,19 @@
const { expect, spy } = require('./test-utils')
// Require the functions to be tested
const { capitalize, capitalizeArray, capitalizeKeys, asyncCapitalize, capitalizeStream } = require('../lib-example/capitalize.js')
const {
capitalize,
capitalizeArray,
capitalizeKeys,
asyncCapitalize,
capitalizeStream
} = require('../lib-example/capitalize.js')
// NOTE: these requires are only there to test arrays
const toStream = require('array-to-stream')
const toArray = require('stream-to-array')
describe('text-utils#capitalize', () => {
it('should uppercase first letter', () => {
const text = 'hello'
const capitalizedText = capitalize(text)
......@@ -41,7 +46,6 @@ describe('text-utils#capitalize', () => {
})
describe('text-utils#capitalizeArray', () => {
it('should capitalize all items of an array', () => {
const array = ['zerO', 'onE', 'twO', 'threE']
// if the expected result is used many times, consider using a variable
......@@ -55,7 +59,6 @@ describe('text-utils#capitalizeArray', () => {
})
describe('text-utils#capitalizeKeys', () => {
let object
let expectedObject
......@@ -85,7 +88,6 @@ describe('text-utils#capitalizeKeys', () => {
})
describe('text-utils#asyncCapitalize', () => {
// try to avoid async/await in tests
// The API from "chai-as-promised" like #to.eventually.equal() gives better logs
it('should uppercase first letter AND lowercase other letters', () => {
......@@ -103,7 +105,6 @@ describe('text-utils#asyncCapitalize', () => {
})
describe('text-utils#capitalizeStream', () => {
let endCallback
let errorCallback
......@@ -123,7 +124,8 @@ describe('text-utils#capitalizeStream', () => {
// it's easier to convert streams to arrays to test their result
const capitalizedArray = toArray(capitalizedStream)
// don't forget to return the promise as the result of the it() test function!
return expect(capitalizedArray).to.be.fulfilled.and.to.eventually.eql(['Zero', 'One', 'Two', 'Three'])
return expect(capitalizedArray)
.to.be.fulfilled.and.to.eventually.eql(['Zero', 'One', 'Two', 'Three'])
.then(() => {
// callback need to be tested asynchronously (after the stream is finished)
expect(errorCallback).not.to.have.been.called()
......@@ -139,7 +141,8 @@ describe('text-utils#capitalizeStream', () => {
.on('end', endCallback)
const capitalizedArray = toArray(capitalizedStream)
// don't forget to return the promise as the result of the it() test function!
return expect(capitalizedArray).to.be.rejectedWith(TypeError)
return expect(capitalizedArray)
.to.be.rejectedWith(TypeError)
.then(() => {
expect(errorCallback).to.have.been.called.once()
expect(endCallback).not.to.have.been.called.once()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment