班表小幫手
驗證班表是否合法或過勞
npm i tw-shift-schedule
這是班表小幫手的核心檢驗邏輯。相關的專案有:
- 前端介面:https://github.com/g0v/tw-shift-schedule-ui
- REST API:https://github.com/poga/tw-shift-schedule-api
Usage
const shift = // 台鐵站長張銘元班表 from https://www.twreporter.org/a/death-of-taiwan-rail-train-conductorlet schedule = shiftSchedule let results = shiftconsole // 檢驗結果
test
資料夾下有其他範例
API
const shift =
let schedule = shift.Schedule.fromTime(times, [opts])
從給定的時間 times
建立一個班表資料。
- times: 二維陣列,每個子元素為「上班時間」與「下班時間」的 pair。如:
[['2018-01-01 08:00:00', '2018-01-01 18:00:00']]
- opts:
- format: 時間的格式,預設為 ISO 8601。格式參考 https://momentjs.com/docs/#/parsing/string/
- before: 隱藏工時-前。ex. '30 minutes'
- after: 隱藏工時-後。ex. '30 minutes'
let data = schedule.toString()
將 schedule 物件轉為字串
let schedule = shift.Schedule.load(data)
從字串建立 schedule 物件。
let errorsAndWarnings = shift.validate(schedule, opts)
解析班表並且回傳班表是否違法。
- schedule: shift.schedule 建立的班表資料。
- opts:
- transformed: 變形工時種類,如果不是變形工時就不填。可為:
shift.validate.two_week
:兩週變形工時shift.validate.four_week
:四週變形工時shift.validate.eight_week
:八週變形工時
- min_between_shift: 兩班之間最少休息時間(小時),預設為 8,根據勞基法可因為勞資協商而介於 8~11 之間。
- transformed: 變形工時種類,如果不是變形工時就不填。可為:
回傳值:
[
{type: 'warning', msg: '警告內容'},
{type: 'error', msg: '錯誤原因', offset: <相對於班表起始時間的位移,單位為分鐘>, time: <錯誤時間>}
]
let causes = shift.overwork.check(schedule)
檢查班表是否符合過勞因素,回傳符合的因素,若是沒有符合的則回傳空陣列。
shift.overwork.Causes
過勞因素:
const Causes = irregular: '不規律的工作' longHours: '長時間工作' nightShift: '夜班' previousDayOverwork: '前一天長時間工作' previousWeekOverwork: '前一週長時間工作' previousMonthOvertime: '前一個月加班時數 > 100' previousSixMonthsOvertime: '前六個月加班時數平均 > 45'
shift.create(employeeCount, shifts)
根據給定的資訊排出合法的班表。
let schedules = // schedule == [[['2018-09-08 09:00:00', '2018-09-08 15:00:00']]]
Internal Data Structure
此套件將班表編碼成如下格式:
xxxxxxxxxx xxxxxxxxx .....xxxxx .....
x
代表一分鐘的工作時間.
代表一分鐘的休息時間
於是便可用 lexer
驗證基本的班表正確性,可參考 lexer.js
。
編碼中的空白會被忽略,可用 #
寫註解。
License
The MIT License