diff --git a/implementations/join.js b/implementations/join.js new file mode 100644 index 0000000..082a49d --- /dev/null +++ b/implementations/join.js @@ -0,0 +1,62 @@ +/** + * + MDN Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join + + The join() method concats every element in array + with a defined separator and returns a new string. + + Example 1 - Without a separator as parameter: + ['a', 'e', 'i', 'o', 'u'].join(); + must return: + 'a,e,i,o,u' + + Example 2 - With a undefined as parameter: + ['a', 'e', 'i', 'o', 'u'].join(undefined); + must return: + 'a,e,i,o,u' + + Example 3 - With a empty string as parameter + ['a', 'e', 'i', 'o', 'u'].join(''); + must return: + 'aeiou' + + Example 4 - With a string as parameter + ['a', 'e', 'i', 'o', 'u'].join('-'); + must return: + 'a-e-i-o-u' + + Example 5 - With a function as parameter + ['a', 'e', 'i', 'o', 'u'].join(() => {}); + must return: + 'a() => {}e() => {}i() => {}o() => {}u' + + Example 6 - With a object as parameter + ['a', 'e', 'i', 'o', 'u'].join({}); + must return: + 'a[object Object]e[object Object]i[object Object]o[object Object]u' + + Example 7 - With an array as parameter + ['a', 'e', 'i', 'o', 'u'].join([1, 2, 3]); + must return: + 'a1,2,3e1,2,3i1,2,3o1,2,3u' + + Example 8 - With a boolean as parameter + ['a', 'e', 'i', 'o', 'u'].join(true); + must return: + 'atrueetrueitrueotrueu' + */ + +Array.prototype.join = function join(separator) { + let returnValue = ''; + let sep = separator; + + if (Object.prototype.toString.call(sep) === '[object Undefined]') { + sep = ','; + } + + for (let index = 0; index < this.length; index += 1) { + returnValue += this[index] + (index < this.length - 1 ? sep : ''); + } + + return returnValue; +}; diff --git a/implementations/join.spec.js b/implementations/join.spec.js new file mode 100644 index 0000000..26c94a3 --- /dev/null +++ b/implementations/join.spec.js @@ -0,0 +1,53 @@ +require('./join'); + +describe('join', () => { + const data = ['a', 'e', 'i', 'o', 'u']; + + describe('when doesn\'t pass parameters', () => { + test('return \'a,e,i,o,u\'', () => { + expect(data.join()).toBe('a,e,i,o,u'); + }); + }); + + describe('when pass \'undefined\' as parameter', () => { + test('return \'a,e,i,o,u\'', () => { + expect(data.join(undefined)).toBe('a,e,i,o,u'); + }); + }); + + describe('when pass \'\' as parameter', () => { + test('return \'aeiou\'', () => { + expect(data.join('')).toBe('aeiou'); + }); + }); + + describe('when pass a string as parameter', () => { + test('return \'a-e-i-o-u\'', () => { + expect(data.join('-')).toBe('a-e-i-o-u'); + }); + }); + + describe('when pass a function as parameter', () => { + test('return \'a() => {}e() => {}i() => {}o() => {}u\'', () => { + expect(data.join(() => {})).toBe('a() => {}e() => {}i() => {}o() => {}u'); + }); + }); + + describe('when pass a object as parameter', () => { + test('return \'a[object Object]e[object Object]i[object Object]o[object Object]u\'', () => { + expect(data.join({})).toBe('a[object Object]e[object Object]i[object Object]o[object Object]u'); + }); + }); + + describe('when pass an array as parameter', () => { + test('return \'a1,2,3e1,2,3i1,2,3o1,2,3u\'', () => { + expect(data.join([1, 2, 3])).toBe('a1,2,3e1,2,3i1,2,3o1,2,3u'); + }); + }); + + describe('when pass a boolean as parameter', () => { + test('return \'atrueetrueitrueotrueu\'', () => { + expect(data.join(true)).toBe('atrueetrueitrueotrueu'); + }); + }); +});