├── .gitignore ├── .vscode └── settings.json ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Suraj Elapully 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # time-slots-generator 2 | [![NPM version](https://img.shields.io/npm/v/time-slots-generator.svg)](https://www.npmjs.com/package/time-slots-generator) 3 | [![Open Source Love](https://badges.frapsoft.com/os/mit/mit.svg?v=102)](https://github.com/ellerbrock/open-source-badge/) 4 | ### A time slot generator with ability to filter out blocked times. 5 | 6 | Written in ES5 for maximum compatibility. 7 | 8 | ## Install 9 | ``` 10 | npm install time-slots-generator --save 11 | ``` 12 | ## Usage 13 | ``` 14 | var ts=require("time-slots-generator"); 15 | 16 | ts.getTimeSlots(blockTimes, showTimeAsString, timeInterval, includeStartBlockedTime, includeEndBlockedTime) 17 | ``` 18 | - blockTimes 19 | - `Array of start and end times` 20 | - ` default: []` 21 | - Use when you want some times periods to be removed(in cases where an appointment is already booked) 22 | 23 | ``` 24 | // time format 25 | // 4:30 ==> 4*60+30 = 270 26 | [ 27 | [270,360], // start and end time 28 | [70,1200] 29 | ] 30 | ``` 31 | 32 | - showTimeAsString 33 | - `Boolean` 34 | - ` default: false` 35 | - returns the output as a map of time and string 36 | 37 | `Eg: {270:'4:30'}` 38 | 39 | - timeInterval 40 | - `string` 41 | - ` default: hour` 42 | - splits the time interval based on the string sent, allowed strings are 43 | 44 | `["tenth","quarter","half","one","two","three","four"]` 45 | 46 | - includeStartBlockedTime 47 | - `Boolean` 48 | - ` default: false` 49 | - includes the start times from blockedTimes in the generated array. 50 | 51 | - includeEndBlockedTime 52 | - `Boolean` 53 | - ` default: false` 54 | - includes the end times from blockedTimes in the generated array. 55 | 56 | 57 | ### Full Example 58 | ```javascript 59 | 60 | const tc = require("time-slots-generator"); 61 | 62 | console.log("get me all the time slots of the day \n",tc.getTimeSlots([],false)); 63 | console.log("get me all the time slots of the day without the given times \n",tc.getTimeSlots([[300,1080]],false)); 64 | 65 | console.log("get me all the time slots of the day with time \n",tc.getTimeSlots([],true)); 66 | console.log("get me all the time slots of the day without the given times with time \n",tc.getTimeSlots([[300,1080]],true)); 67 | 68 | console.log("get me all the time slots of the day with time in 2hr intervals\n",tc.getTimeSlots([],true,"two")); 69 | console.log("get me all the time slots of the day without the given times with time 15min intervals\n",tc.getTimeSlots([[300,1080]],true,"quarter")); 70 | 71 | 72 | $ node index.js 73 | get me all the time slots of the day 74 | [ 60, 75 | 120, 76 | 180, 77 | 240, 78 | 300, 79 | 360, 80 | 420, 81 | 480, 82 | 540, 83 | 600, 84 | 660, 85 | 720, 86 | 780, 87 | 840, 88 | 900, 89 | 960, 90 | 1020, 91 | 1080, 92 | 1140, 93 | 1200, 94 | 1260, 95 | 1320, 96 | 1380, 97 | 1440 ] 98 | 99 | get me all the time slots of the day without the given times 100 | [ 60, 120, 180, 240, 1140, 1200, 1260, 1320, 1380, 1440 ] 101 | 102 | get me all the time slots of the day with time 103 | { '60': '1:00', 104 | '120': '2:00', 105 | '180': '3:00', 106 | '240': '4:00', 107 | '300': '5:00', 108 | '360': '6:00', 109 | '420': '7:00', 110 | '480': '8:00', 111 | '540': '9:00', 112 | '600': '10:00', 113 | '660': '11:00', 114 | '720': '12:00', 115 | '780': '13:00', 116 | '840': '14:00', 117 | '900': '15:00', 118 | '960': '16:00', 119 | '1020': '17:00', 120 | '1080': '18:00', 121 | '1140': '19:00', 122 | '1200': '20:00', 123 | '1260': '21:00', 124 | '1320': '22:00', 125 | '1380': '23:00', 126 | '1440': '24:00' } 127 | 128 | get me all the time slots of the day without the given times with time 129 | { '60': '1:00', 130 | '120': '2:00', 131 | '180': '3:00', 132 | '240': '4:00', 133 | '1140': '19:00', 134 | '1200': '20:00', 135 | '1260': '21:00', 136 | '1320': '22:00', 137 | '1380': '23:00', 138 | '1440': '24:00' } 139 | 140 | get me all the time slots of the day with time in 2hr intervals 141 | { '120': '2:00', 142 | '240': '4:00', 143 | '360': '6:00', 144 | '480': '8:00', 145 | '600': '10:00', 146 | '720': '12:00', 147 | '840': '14:00', 148 | '960': '16:00', 149 | '1080': '18:00', 150 | '1200': '20:00', 151 | '1320': '22:00', 152 | '1440': '24:00' } 153 | 154 | get me all the time slots of the day without the given times with time 15min intervals 155 | { '15': '0:15', 156 | '30': '0:30', 157 | '45': '0:45', 158 | '60': '1:00', 159 | '75': '1:15', 160 | '90': '1:30', 161 | '105': '1:45', 162 | '120': '2:00', 163 | '135': '2:15', 164 | '150': '2:30', 165 | '165': '2:45', 166 | '180': '3:00', 167 | '195': '3:15', 168 | '210': '3:30', 169 | '225': '3:45', 170 | '240': '4:00', 171 | '255': '4:15', 172 | '270': '4:30', 173 | '285': '4:45', 174 | '1095': '18:15', 175 | '1110': '18:30', 176 | '1125': '18:45', 177 | '1140': '19:00', 178 | '1155': '19:15', 179 | '1170': '19:30', 180 | '1185': '19:45', 181 | '1200': '20:00', 182 | '1215': '20:15', 183 | '1230': '20:30', 184 | '1245': '20:45', 185 | '1260': '21:00', 186 | '1275': '21:15', 187 | '1290': '21:30', 188 | '1305': '21:45', 189 | '1320': '22:00', 190 | '1335': '22:15', 191 | '1350': '22:30', 192 | '1365': '22:45', 193 | '1380': '23:00', 194 | '1395': '23:15', 195 | '1410': '23:30', 196 | '1425': '23:45', 197 | '1440': '24:00' } 198 | 199 | ``` 200 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | function getTime(num) { 2 | var tempHour = String(Math.trunc(num / 60)); 3 | var hour = tempHour + "".length === 1 ? "0" + tempHour : tempHour; 4 | var min = num % 60 === 0 ? "00" : num % 60; 5 | return { num: num, time: hour + ":" + min }; 6 | } 7 | function getTimeSlots(blockTimes, showTimeAsString, interval, includeStartBlockedTime,includeEndBlockedTime) { 8 | var times = 1, 9 | sums = 60; 10 | includeStartBlockedTime = includeStartBlockedTime === true ? true : false; 11 | includeEndBlockedTime = includeEndBlockedTime === true ? true : false; 12 | switch (interval) { 13 | case "tenth": 14 | times = 6; 15 | sums = 10; 16 | break; 17 | case "quarter": 18 | times = 4; 19 | sums = 15; 20 | break; 21 | case "half": 22 | times = 2; 23 | sums = 30; 24 | break; 25 | case "one": 26 | times = 1; 27 | sums = 60; 28 | break; 29 | case "two": 30 | times = 1 / 2; 31 | sums = 120; 32 | break; 33 | case "three": 34 | times = 1 / 3; 35 | sums = 180; 36 | break; 37 | case "four": 38 | times = 1 / 4; 39 | sums = 240; 40 | break; 41 | default: 42 | times = 1; 43 | sums = 60; 44 | break; 45 | } 46 | var start = 0; 47 | var dateTimes = Array(Math.round(24 * times)) 48 | .fill(0) 49 | .map(function(_) { 50 | start = start + sums; 51 | return start; 52 | }); 53 | blockTimes = Array.isArray(blockTimes) === true && blockTimes.length > 0 ? blockTimes : []; 54 | if (blockTimes.length > 0) { 55 | dateTimes = blockTimes.reduce(function(acc, x) { 56 | return acc 57 | .filter(function(y) { 58 | return includeStartBlockedTime == true ? y <= x[0] : y < x[0]; 59 | }) 60 | .concat( 61 | acc.filter(function(y) { 62 | return includeEndBlockedTime == true ? y >= x[1] : y > x[1]; 63 | }) 64 | ); 65 | }, dateTimes); 66 | } 67 | if (showTimeAsString === true) { 68 | return dateTimes 69 | .map(function(x) { 70 | return getTime(x); 71 | }) 72 | .reduce(function(accc, element) { 73 | accc["" + element.num] = element.time; 74 | return accc; 75 | }, {}); 76 | } 77 | return dateTimes; 78 | } 79 | 80 | module.exports = { 81 | getTimeSlots: getTimeSlots 82 | }; 83 | 84 | // console.log(getTimeSlots([[340, 550], [920, 1240]], true, "tenth")); 85 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "time-slots-generator", 3 | "version": "1.0.6", 4 | "description": "a time slot generator with ability to filter out blocked times", 5 | "main": "index.js", 6 | "keywords": [ 7 | "time-slots", 8 | "blocked", 9 | "schedule", 10 | "appointments", 11 | "time", 12 | "slot" 13 | ], 14 | "repository": "github:essuraj/time-slots-generator", 15 | "author": "Suraj Elapully", 16 | "license": "MIT" 17 | } 18 | --------------------------------------------------------------------------------