facebook-opening-hours-to-table
Version:
Generates an html table from Facebook page opening hours data, such that days with equal opening hours are on one table row.
162 lines (137 loc) • 5.43 kB
Markdown
Generates an html table from Facebook page opening hours data, such that days with equal opening hours are on one table row.
[](https://travis-ci.org/calvinclaus/facebook-opening-hours-to-table)
[](https://codecov.io/github/calvinclaus/facebook-opening-hours-to-table)
[](http://npm.im/facebook-opening-hours-to-table)
[](http://npm-stat.com/charts.html?package=facebook-opening-hours-to-table&from=2015-08-01)
[](http://opensource.org/licenses/MIT)

This package is distributed via npm:
```
npm i facebook-opening-hours-to-table
```
It has no dependencies.
Get the file from [https://npmcdn.com/facebook-opening-hours-to-table/build](https://npmcdn.com/facebook-opening-hours-to-table/build) and put it in a script tag:
```html
<script src="entry.umd.min.js"></script>
//Functions now at facebookOpeningHoursToTable.getTableWithHours(hours, translationDict);
```
Facebook hours-data looks like this:
```json
{
"mon_1_open": "11:00",
"mon_1_close": "20:30",
"tue_1_open": "11:00",
"tue_1_close": "20:30",
"wed_1_open": "11:00",
"wed_1_close": "20:30",
"thu_1_open": "11:00",
"thu_1_close": "21:00",
"fri_1_open": "11:00",
"fri_1_close": "21:00",
"sat_1_open": "10:30",
"sat_1_close": "20:30",
"sun_1_open": "12:00",
"sun_1_close": "19:00"
}
```
And this library creates:
<table><tbody><tr><td><span class="days">mon - wed: </span></td><td><span class="numbers">11:00 - 20:30</span></td></tr><tr><td><span class="days">thu - fri: </span></td><td><span class="numbers">11:00 - 21:00</span></td></tr><tr><td><span class="days">sat: </span></td><td><span class="numbers">10:30 - 20:30</span></td></tr><tr><td><span class="days">sun: </span></td><td><span class="numbers">12:00 - 19:00</span></td></tr></tbody></table>
Use this method if you want an html table.
Fetch the opening hours data from Facebook and pass it to getTableWithHours.
```javascript
import { getTableWithHours } from Facebook-opening-hours-to-table;
//...
const table = getTableWithHoursData(yourFBHoursData, translationDict);
//do stuff with table
```
Use this method if you do not care for the table and just want the [intervals](
Fetch the opening hours data from Facebook and pass it to getIntervalsWithHours to get to the raw [intervals](
```javascript
import { getIntervalsWithHours } from facebook-opening-hours-to-table;
//...
var intervals = getIntervalsWithHours(yourFBHoursData, translationDict);
//do stuff with intervals
```
Intervals is an array of intervals that is created form the opening hours data. You can use it, if you do not want a table to be generated from the Facebook hours data, but still want days with equal opening hours be compressed into one array entry:
```javascript
//This is how intervals looks like
var intervals = [
{
"open": "11:00",
"close": "20:30",
"from": "mon",
"to": "wed"
},
{
"open": "11:00",
"close": "21:00",
"from": "thu",
"to": "fri"
},
{
"open": "10:30",
"close": "20:30",
"from": "sat",
"to": "sat"
},
{
"open": "12:00",
"close": "19:00",
"from": "sun",
"to": "sun"
}
]
//Generated from:
var hours = {
"mon_1_open": "11:00",
"mon_1_close": "20:30",
"tue_1_open": "11:00",
"tue_1_close": "20:30",
"wed_1_open": "11:00",
"wed_1_close": "20:30",
"thu_1_open": "11:00",
"thu_1_close": "21:00",
"fri_1_open": "11:00",
"fri_1_close": "21:00",
"sat_1_open": "10:30",
"sat_1_close": "20:30",
"sun_1_open": "12:00",
"sun_1_close": "19:00"
}
```
Each method takes translationDict as an argument. This argument can be omitted if you are happy with Facebooks default "mon, tue,...".
If not, you can supply a dictionary like so:
```javascript
translationDict = {mon: "Mo", tue: "Di", wed: "Mi", thu: "Do", fri: "Fr", sat: "Sa", sun: "So"};
```
```javascript
$.ajax({
method: "GET",
url: "https://graph.facebook.com/QuellenStr", //Schnitzelhaus Vienna :)
data : {
access_token: token,
fields: "hours"
}
})
.done(function( msg ) {
var hours = msg.hours;
var translationDict = {mon: "Mo", tue: "Di", wed: "Mi", thu: "Do", fri: "Fr", sat: "Sa", sun: "So"};
var table = facebookOpeningHoursToTable.getTableWithHours(hours, translationDict);
document.body.appendChild(table);
})
```
*Note: you can use any ajax-request library. I chose to use jQuery in this example. This library does NOT depend on jQuery*
If this helped you out, make my day by [letting me know](https://twitter.com/calvin_claus)!
This library was crafted with care by [Calvin Claus](https://twitter.com/calvin_claus).