Skip to content
This repository has been archived by the owner on Dec 24, 2024. It is now read-only.

Challenge Accepted #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
env:
browser: true
commonjs: true
es6: true
extends:
- standard
globals:
Atomics: readonly
SharedArrayBuffer: readonly
parserOptions:
ecmaVersion: 2018
rules: {
no-useless-catch: off,
no-extend-native: off,
no-unused-expressions: off
}
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# package directories
node_modules
jspm_packages

# Serverless directories
.serverless

#tests
.nyc_output/
coverage/
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
# hiring-tests
This repo is designated to our hiring tests.
# Execução
Para fazer a execução deste projeto basta executar o seguinte comando:
```
sls invoke local --function sort --data '{[3, 5, 6, 1, 2, 16], [16, 6, 91, 1, 4, 3, 123, 1, 1]}'
```
os valor entre aspas após o termo `--data` é a entrada de dados, usando o mesmo formato sugerido no desafio.

# Testes

Para a execução dos testes pode-se utilizar dois comandos:

```
npm run test
```

ou

```
npm run test:coverage
```

O primeiro comando só executa os testes o segundo comando executa os testes e calcula a cobertura de código.

# Lint

Assim como nos testes existem dois comandos para verificação do lint:

```
npm run lint
```

ou

```
npm run lint:fix
```

O primeiro simplesmente analisa erros de lint, já o segundo comando analisa e resolve problemas que podem ser resolvidos de forma automática.

56 changes: 56 additions & 0 deletions handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict'

/**
* @function String.prototype.replaceAll
* Add to String prototype the replaceAll function
* @param {string} needle
* @return {string} replacement
*/
String.prototype.replaceAll = function (needle, replacement) {
return this.split(needle).join(replacement)
}

/**
* @function mergeArrayAndSort
* Convert a string like "{[3, 5, 6, 1, 2, 16], [16, 6, 91, 1, 4, 3, 123, 1, 1]}"
* to sorted asc array
* @param {string} str - Input string
* @return {array} arr - Array sorted
*/
const mergeArraysAndSort = (str) => {
try {
return str
.replaceAll('{[', '')
.replaceAll(']', '')
.replaceAll('[', '')
.replaceAll('{', '')
.replaceAll('}', '')
.replaceAll(']}', '')
.split(',')
.map(item => parseInt(item, 10))
.sort((a, b) => a - b)
} catch (error) {
throw error
}
}

/**
* @function getDuplicatedArray
* Get duplicated values in integer array sorted
* @param {arr} arr - Array asc sorted
* @return {array} duplicated - Array asc sorted of duplicated values
*/
const getDuplicatedArray = (arr) => {
const result = []
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i + 1] === arr[i] && !result.includes(arr[i])) {
result.push(arr[i])
}
}

return result
}

module.exports.sort = async event =>
`${getDuplicatedArray(mergeArraysAndSort(event))}`
.replaceAll(',', ', ')
Loading