Skip to content

Instantly share code, notes, and snippets.

@alvarowolfx
Created September 28, 2017 02:04
Show Gist options
  • Save alvarowolfx/edace611ba52c66a0389282b58d166fd to your computer and use it in GitHub Desktop.
Save alvarowolfx/edace611ba52c66a0389282b58d166fd to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/dididupafo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
'use strict';
var days = ['Domingo','Segunda','Terça','Quarta','Quinta', 'Sexta', 'Sábado'];
function stringWithFirstAndEnd(first, end, offset){
if(first === undefined){
return null;
}
if(end === undefined){
return days[first + offset]
}
return "de " + days[offset+first] + " a " + days[offset+end];
}
function aggregateDays(data, offset){
if(offset === undefined){
offset = 0
}
let first, end;
for(let i = 0; i < 7; i++){
if(data[i]){
if(first === undefined){
first = i;
}else{
end = i;
}
}else if(first !== undefined){
let newOffset = i + offset;
let rest = aggregateDays(data.slice(i), newOffset);
let current = stringWithFirstAndEnd(first,end,offset);
if(!rest){
return current;
}
return current + " / " + rest;
}
}
return stringWithFirstAndEnd(first,end,offset)
}
let db = [null, '08:00','08:00','08:00','08:00','08:00', null];
let db1 = [null, null,'08:00','08:00','08:00','08:00', '08:00'];
let db2 = [null, '08:00','08:00','08:00','08:00',null, null];
let db3 = ['08:00', '08:00','08:00',null,'08:00',null, null];
let db4 = ['08:00', '08:00','08:00',null,'08:00','08:00', '08:00'];
let db5 = ['08:00', '08:00',null,null,'08:00',null, '08:00'];
let db6 = ['08:00', null,'08:00',null,'08:00',null, '08:00'];
console.log(aggregateDays(db));
console.log(aggregateDays(db1));
console.log(aggregateDays(db2));
console.log(aggregateDays(db3));
console.log(aggregateDays(db4));
console.log(aggregateDays(db5));
console.log(aggregateDays(db6));
</script>
<script id="jsbin-source-javascript" type="text/javascript">'use strict';
var days = ['Domingo','Segunda','Terça','Quarta','Quinta', 'Sexta', 'Sábado'];
function stringWithFirstAndEnd(first, end, offset){
if(first === undefined){
return null;
}
if(end === undefined){
return days[first + offset]
}
return "de " + days[offset+first] + " a " + days[offset+end];
}
function aggregateDays(data, offset){
if(offset === undefined){
offset = 0
}
let first, end;
for(let i = 0; i < 7; i++){
if(data[i]){
if(first === undefined){
first = i;
}else{
end = i;
}
}else if(first !== undefined){
let newOffset = i + offset;
let rest = aggregateDays(data.slice(i), newOffset);
let current = stringWithFirstAndEnd(first,end,offset);
if(!rest){
return current;
}
return current + " / " + rest;
}
}
return stringWithFirstAndEnd(first,end,offset)
}
let db = [null, '08:00','08:00','08:00','08:00','08:00', null];
let db1 = [null, null,'08:00','08:00','08:00','08:00', '08:00'];
let db2 = [null, '08:00','08:00','08:00','08:00',null, null];
let db3 = ['08:00', '08:00','08:00',null,'08:00',null, null];
let db4 = ['08:00', '08:00','08:00',null,'08:00','08:00', '08:00'];
let db5 = ['08:00', '08:00',null,null,'08:00',null, '08:00'];
let db6 = ['08:00', null,'08:00',null,'08:00',null, '08:00'];
console.log(aggregateDays(db));
console.log(aggregateDays(db1));
console.log(aggregateDays(db2));
console.log(aggregateDays(db3));
console.log(aggregateDays(db4));
console.log(aggregateDays(db5));
console.log(aggregateDays(db6));</script></body>
</html>
'use strict';
var days = ['Domingo','Segunda','Terça','Quarta','Quinta', 'Sexta', 'Sábado'];
function stringWithFirstAndEnd(first, end, offset){
if(first === undefined){
return null;
}
if(end === undefined){
return days[first + offset]
}
return "de " + days[offset+first] + " a " + days[offset+end];
}
function aggregateDays(data, offset){
if(offset === undefined){
offset = 0
}
let first, end;
for(let i = 0; i < 7; i++){
if(data[i]){
if(first === undefined){
first = i;
}else{
end = i;
}
}else if(first !== undefined){
let newOffset = i + offset;
let rest = aggregateDays(data.slice(i), newOffset);
let current = stringWithFirstAndEnd(first,end,offset);
if(!rest){
return current;
}
return current + " / " + rest;
}
}
return stringWithFirstAndEnd(first,end,offset)
}
let db = [null, '08:00','08:00','08:00','08:00','08:00', null];
let db1 = [null, null,'08:00','08:00','08:00','08:00', '08:00'];
let db2 = [null, '08:00','08:00','08:00','08:00',null, null];
let db3 = ['08:00', '08:00','08:00',null,'08:00',null, null];
let db4 = ['08:00', '08:00','08:00',null,'08:00','08:00', '08:00'];
let db5 = ['08:00', '08:00',null,null,'08:00',null, '08:00'];
let db6 = ['08:00', null,'08:00',null,'08:00',null, '08:00'];
console.log(aggregateDays(db));
console.log(aggregateDays(db1));
console.log(aggregateDays(db2));
console.log(aggregateDays(db3));
console.log(aggregateDays(db4));
console.log(aggregateDays(db5));
console.log(aggregateDays(db6));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment