date-utils-2020
Advanced tools
Comparing version 1.0.3 to 1.1.0
@@ -13,5 +13,2 @@ /** | ||
parserOptions: { | ||
parser: 'babel-eslint', | ||
// https://eslint.org/docs/rules/rest-spread-spacing | ||
// error Parsing error: Unexpected token .. | ||
ecmaVersion: 2020, | ||
@@ -21,3 +18,3 @@ sourceType: 'module' | ||
extends: [ | ||
'standard', | ||
// 'standard', | ||
'plugin:@typescript-eslint/recommended' | ||
@@ -24,0 +21,0 @@ ], |
{ | ||
"name": "date-utils-2020", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "date utils 2020, format(date: Date, formatter: string)/toDate(a?: any)...", | ||
"main": "dist/date-utils-2020.js", | ||
"main": "dist/date-utils-2020.umd.js", | ||
"module": "dist/date-utils-2020.es.js", | ||
"exports": { | ||
".": { | ||
"import": "./dist/date-utils-2020.es.js", | ||
"require": "./dist/date-utils-2020.umd.js" | ||
} | ||
}, | ||
"scripts": { | ||
"dev": "webpack serve", | ||
"build": "webpack --mode production", | ||
"test": "npm run build && ts-node test/index.ts" | ||
"dev": "vite", | ||
"build": "vite build", | ||
"test": "vitest", | ||
"lint": "eslint . --fix --ext .ts" | ||
}, | ||
@@ -30,26 +38,9 @@ "types": "types/index.d.ts", | ||
"devDependencies": { | ||
"@babel/core": "^7.12.9", | ||
"@babel/plugin-transform-typescript": "^7.12.1", | ||
"@babel/preset-env": "^7.12.7", | ||
"@babel/preset-typescript": "^7.12.7", | ||
"@typescript-eslint/eslint-plugin": "^4.11.1", | ||
"@typescript-eslint/parser": "^4.11.1", | ||
"babel-loader": "^8.2.2", | ||
"clean-webpack-plugin": "^3.0.0", | ||
"eslint": "^7.14.0", | ||
"eslint-config-standard": "^16.0.2", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"eslint-plugin-standard": "^5.0.0", | ||
"eslint-webpack-plugin": "^2.4.1", | ||
"html-webpack-plugin": "^4.5.0", | ||
"ts-loader": "^8.0.13", | ||
"ts-node": "^9.1.1", | ||
"typescript": "^4.1.3", | ||
"webpack": "^5.9.0", | ||
"webpack-cli": "^4.2.0", | ||
"webpack-dev-server": "^3.11.0", | ||
"webpack-merge": "^5.4.0" | ||
"vite": "^4.0.4", | ||
"vitest": "^0.27.1" | ||
} | ||
} |
@@ -11,4 +11,4 @@ /** | ||
*/ | ||
export function toTwoDigits(str: string): string { | ||
return str[1] ? str : '0' + str | ||
export function toTwoDigits(str: string | number): string { | ||
return String(str).padStart(2, '0') | ||
} |
@@ -7,5 +7,9 @@ /** | ||
import { toTwoDigits } from './helper' | ||
import * as Types from '../types/index' | ||
const DEF_LANGUAGE: Types.ILangPackage = { | ||
export interface ILangPackage { | ||
weeks: string[], | ||
[key: string]: any | ||
} | ||
const DEF_LANGUAGE: ILangPackage = { | ||
// weeks: ['日', '一', '二', '三', '四', '五', '六'] | ||
@@ -17,3 +21,3 @@ weeks: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] | ||
* format date | ||
* @param srcDate | ||
* @param input | ||
* @param fmt | ||
@@ -23,11 +27,11 @@ * @param langPackage | ||
*/ | ||
function formatDate<T>(srcDate: T, fmt: string, langPackage?: Types.ILangPackage): string { | ||
const date = toDate(srcDate) | ||
if (!date || !fmt) return srcDate + '' | ||
function formatDate<T>(input: T, fmt: string, langPackage?: ILangPackage): string { | ||
const date = toDate(input) | ||
if (!date || !fmt) return String(input) | ||
// timestamp | ||
if (fmt === 'timestamp') return date.getTime().toString() | ||
let $1 | ||
if (/(y+)/i.test(fmt)) { | ||
$1 = RegExp.$1 | ||
fmt = fmt.replace($1, (date.getFullYear() + '').substr(4 - $1.length)) | ||
const $1 = RegExp.$1 | ||
fmt = fmt.replace($1, (date.getFullYear() + '').substring(4 - $1.length)) | ||
} | ||
@@ -45,6 +49,6 @@ | ||
's+': date.getSeconds(), | ||
// week number | ||
'w+': date.getDay(), | ||
// week text | ||
'W+': langPackage.weeks[date.getDay()], | ||
// // week number | ||
// 'w+': date.getDay(), | ||
// // week text | ||
// 'W+': langPackage.weeks[date.getDay()], | ||
// am/pm | ||
@@ -55,6 +59,7 @@ 'a+': date.getHours() < 12 ? 'am' : 'pm', | ||
let $1 | ||
for (const key in obj) { | ||
if (new RegExp('(' + key + ')').test(fmt)) { | ||
$1 = RegExp.$1 | ||
const str = obj[key] + '' | ||
const str = obj[key as keyof typeof obj] + '' | ||
fmt = fmt.replace($1, ($1.length === 1) ? str : toTwoDigits(str)) | ||
@@ -64,2 +69,8 @@ } | ||
// week | ||
if (/w+/i.test(fmt)) { | ||
const w = date.getDay() | ||
fmt = fmt.replace(/w+/i, /W+/.test(fmt) ? langPackage.weeks[w] : String(w)) | ||
} | ||
// GMT(Greenwich Mean Time) | ||
@@ -69,3 +80,3 @@ // Chrome: Sun Aug 01 2021 14:20:04 GMT+0900 (Japan Standard Time) | ||
// Safari: Sun Aug 01 2021 14:37:08 GMT+0900 (JST) | ||
if (/(g)/i.test(fmt)) { | ||
if (/g/i.test(fmt)) { | ||
const gmt = date.toString().split(/\s+/).slice(5) | ||
@@ -85,8 +96,14 @@ const isLowerCase = fmt.includes('g') | ||
function toDate<T>(input: T): null | Date { | ||
if (input instanceof Date) return input | ||
let result = null | ||
if (input instanceof Date) { | ||
result = input | ||
} | ||
// fix: In the case of an array with only one element | ||
// Example: ['2021/01/02'].toString() => '2021/01/02' | ||
if (typeof input === 'number') { | ||
return new Date(input) | ||
} else if (typeof input === 'string') { | ||
else if (typeof input === 'number') { | ||
// timestamp | ||
result = new Date(input) | ||
} | ||
// string | ||
else if (typeof input === 'string') { | ||
let str = input.trim() | ||
@@ -98,11 +115,11 @@ // string number | ||
if (len === 8) { | ||
return new Date([str.substr(0, 4), str.substr(4, 2), str.substr(6, 2)].join('/')) | ||
result = new Date([str.substring(0, 4), str.substring(4, 6), str.substring(6, 8)].join('/')) | ||
} | ||
// yyyyMM | ||
else if (len === 6) { | ||
return new Date([str.substr(0, 4), str.substr(4, 2), '01'].join('/')) | ||
result = new Date([str.substring(0, 4), str.substring(4, 6), '01'].join('/')) | ||
} | ||
// yyyy | ||
else if (len === 4) { | ||
return new Date(str + '/01/01') | ||
result = new Date(str + '/01/01') | ||
} | ||
@@ -112,3 +129,3 @@ // Other cases are handled as timestamp | ||
// Note that the results of new Date(0) and new Date('0') are different | ||
return new Date(parseInt(input)) | ||
result = new Date(parseInt(input)) | ||
} | ||
@@ -129,14 +146,13 @@ } else { | ||
if (/^(\d{4})[-/](\d{1,2})[-/](\d{1,2})$/.test(str)) { | ||
return new Date([RegExp.$1, RegExp.$2, RegExp.$3].join('/')) | ||
result = new Date([RegExp.$1, RegExp.$2, RegExp.$3].join('/')) | ||
} | ||
/** yyyy/MM yyyy-MM */ | ||
else if (/^(\d{4})[-/](\d{1,2})$/.test(str)) { | ||
return new Date([RegExp.$1, RegExp.$2, '01'].join('/')) | ||
result = new Date([RegExp.$1, RegExp.$2, '01'].join('/')) | ||
} else { | ||
const date = new Date(str) | ||
return isNaN(date.getFullYear()) ? null : date | ||
result = new Date(str) | ||
} | ||
} | ||
} | ||
return null | ||
return result && !isNaN(result.getFullYear()) ? result : null | ||
} | ||
@@ -143,0 +159,0 @@ |
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es5", | ||
"sourceMap": true | ||
"target": "ESNext", | ||
"useDefineForClassFields": true, | ||
"lib": ["DOM", "DOM.Iterable", "ESNext"], | ||
"allowJs": false, | ||
"skipLibCheck": true, | ||
"esModuleInterop": false, | ||
"allowSyntheticDefaultImports": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"module": "ESNext", | ||
"moduleResolution": "Node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"noEmit": true, | ||
"baseUrl": "." | ||
}, | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} | ||
"include": ["src", "types", "tests"] | ||
} |
@@ -6,11 +6,2 @@ /** | ||
*/ | ||
export interface ILangPackage { | ||
weeks: string[], | ||
[key: string]: any | ||
} | ||
export function formatDate(srcDate: any, fmt: string, langPackage?: ILangPackage): string; | ||
export function toDate(date: any): Date | null; | ||
export function toTwoDigits(n: any): string; | ||
export type * from '../src' |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
20467
6
0
100
415
14
1