Created
March 1, 2018 20:21
-
-
Save VitorLuizC/08f9c9c3b838e41555aafae120669fd4 to your computer and use it in GitHub Desktop.
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
| [ | |
| { | |
| "descricao": "PAC", | |
| "itens": [ | |
| { | |
| "freteinvalido": true, | |
| "sku": "2358878.7412.73973", | |
| "valor": 5 | |
| }, | |
| { | |
| "freteinvalido": false, | |
| "sku": "2390933.7658.92431", | |
| "valor": 5 | |
| }, | |
| { | |
| "freteinvalido": false, | |
| "sku": "2390937.51608.280362", | |
| "valor": 5 | |
| } | |
| ], | |
| "prazo": "15 dias úteis" | |
| }, | |
| { | |
| "descricao": "SEDEX", | |
| "itens": [ | |
| { | |
| "freteinvalido": false, | |
| "sku": "2358878.7412.73973", | |
| "valor": 15 | |
| }, | |
| { | |
| "freteinvalido": false, | |
| "sku": "2390933.7658.92431", | |
| "valor": 15 | |
| }, | |
| { | |
| "freteinvalido": false, | |
| "sku": "2390937.51608.280362", | |
| "valor": 15 | |
| } | |
| ], | |
| "prazo": "5 dias úteis" | |
| }, | |
| { | |
| "descricao": "TRANSPORTADORA TRANSPORTES LTDA", | |
| "itens": [ | |
| { | |
| "freteinvalido": false, | |
| "sku": "2358878.7412.73973", | |
| "valor": 637.7 | |
| }, | |
| { | |
| "freteinvalido": true, | |
| "sku": "2390933.7658.92431", | |
| "valor": 0 | |
| }, | |
| { | |
| "freteinvalido": true, | |
| "sku": "2390937.51608.280362", | |
| "valor": 0 | |
| } | |
| ], | |
| "prazo": "10 dias úteis" | |
| } | |
| ] |
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
| const getGroupValidSKUs = (group) => { | |
| const isValid = (item) => !item.freteinvalido | |
| const toSKU = (item) => item.sku | |
| const SKUs = group.itens.filter(isValid).map(toSKU) | |
| return SKUs | |
| } | |
| const isValidGroup = (group, groups) => { | |
| const SKUs = getGroupValidSKUs(group) | |
| if (!SKUs.length) // Um short-circuit pra eliminar grupos onde | |
| return false // nenhum dos items é um frete válido. | |
| const OSKUs = [].concat(...groups.map(getGroupValidSKUs)) | |
| if (!OSKUs.length) // Outro short-circuit pra eliminar grupos | |
| return false // em que não há SKUs válidos pra comparar. | |
| const isRepeated = SKUs.some((SKU) => OSKUs.includes(SKU)) | |
| return isRepeated | |
| } | |
| const getValidGroups = ([group, ...groups]) => { | |
| if (!group || !groups.length) | |
| return [] | |
| const isValid = isValidGroup(group, groups) | |
| if (isValid) | |
| return [ group, ...getValidGroups(groups) ] | |
| return getValidGroups(groups) | |
| } | |
| export default getValidGroups |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment