Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
epszaw committed Feb 18, 2023
0 parents commit 470bd62
Show file tree
Hide file tree
Showing 13 changed files with 1,922 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
50 changes: 50 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Build

on:
push:
branches:
- master
pull_request:
branches:
- "*"

jobs:
test:
strategy:
matrix:
os: [ubuntu-latest]
node_version: [16, 18]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v3

- uses: pnpm/action-setup@v2
name: Install pnpm
id: pnpm-install
with:
version: 7
run_install: false

- name: Get pnpm store directory
id: pnpm-cache
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install

- name: Build the library
run: pnpm build

- name: Test the library
run: pnpm test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
dist
node_modules
10 changes: 10 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"semi": false,
"singleQuote": false,
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"trailingComma": "es5",
"arrowParens": "always",
"bracketSameLine": false
}
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright 2023 Konstantin Epishev <konstantin@epishev.me>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# `dayjs-calendar-month`

> ⏰ Unofficial Day.js plugin to get month calendar range
## Install

Install `dayjs` and the plugin with your favorite package manager:

```shell
npm add dayjs dayjs-calendar-month
```

## Usage

Extend `dayjs`:

```js
import dayjs from "dayjs"
// required to the plugin
import isBetween from "dayjs/plugin/isBetween"
import calendarMonth from "dayjs-calendar-month"

dayjs.extend(isBetween)
dayjs.extend(calendarMonth)

dayjs("2020-01-01").calendarMonth() // array of dayjs instances from 2019-12-29 to 2020-02-01
```
10 changes: 10 additions & 0 deletions dayjs.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PluginFunc } from "dayjs"

declare const plugin: PluginFunc
export = plugin

declare module "dayjs" {
interface Dayjs {
calendarMonth(): Dayjs[]
}
}
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default (o, c) => {
const proto = c.prototype

proto.calendarMonth = function () {
const start = this.startOf("month").startOf("week")
const end = this.endOf("month").endOf("week")
const days = []
let i = start.clone()

while (i.isBetween(start.subtract(1, "second"), end)) {
days.push(i.startOf("day"))
i = i.add(1, "day")
}

return days
}
}
25 changes: 25 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { test, expect } from "vitest"
import dayjs from "dayjs"
import IsBetweenPlugin from "dayjs/plugin/isBetween"
import CalendarMonthPlugin from "./index"

dayjs.extend(IsBetweenPlugin)
dayjs.extend(CalendarMonthPlugin)

test("calendar month multiple of seven", () => {
expect(dayjs("2020-01-01").calendarMonth().length % 7).toBe(0)
})

test("first day of calendar is the first day of the first week of month", () => {
const calendar = dayjs("2020-01-01").calendarMonth()

expect(calendar[0].valueOf()).toBe(dayjs("2019-12-29 00:00:00").valueOf())
})

test("last day of calendar is the last day of the last week of month", () => {
const calendar = dayjs("2020-01-01").calendarMonth()

expect(calendar[calendar.length - 1].valueOf()).toBe(
dayjs("2020-02-01 00:00:00").valueOf()
)
})
54 changes: 54 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "dayjs-calendar-month",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "vitest",
"build": "vite build"
},
"lint-staged": {
"*.{js,json}": [
"prettier --write",
"git add"
]
},
"simple-git-hooks": {
"pre-commit": "npx lint-staged"
},
"repository": {
"type": "git",
"url": "git+https://github.com/epszaw/dayjs-calendar-month.git"
},
"keywords": [],
"author": "Konstantin Epishev <konstantin@epishev.me>",
"license": "MIT",
"bugs": {
"url": "https://github.com/epszaw/dayjs-calendar-month/issues"
},
"homepage": "https://github.com/epszaw/dayjs-calendar-month#readme",
"files": [
"./dist",
"package.json",
"dayjs.d.ts",
"README.md"
],
"types": "./dayjs.d.ts",
"main": "./dist/dayjs-calendar-month.js",
"module": "./dist/dayjs-calendar-month.mjs",
"exports": {
".": {
"require": "./dist/dayjs-calendar-month.js",
"import": "./dist/dayjs-calendar-month.mjs"
}
},
"devDependencies": {
"lint-staged": "^13.1.2",
"prettier": "^2.8.4",
"simple-git-hooks": "^2.8.1",
"vite": "^4.1.2",
"vitest": "^0.28.5"
},
"dependencies": {
"dayjs": "^1.11.7"
}
}
Loading

0 comments on commit 470bd62

Please sign in to comment.