Comparing version 1.0.3 to 2.1.0
122
date-tool.js
@@ -0,20 +1,70 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.getTimeString = getTimeString; | ||
exports.getDateString = getDateString; | ||
exports.getDateString2 = getDateString2; | ||
exports.getShortMonthName = getShortMonthName; | ||
exports.getIsoDateString = getIsoDateString; | ||
exports.parseIsoDateString = parseIsoDateString; | ||
exports.throttle = throttle; | ||
exports.debounce = debounce; | ||
var isoDateRegex = /^(?:(?:([\d]{4})-)?(0?[1-9]|1[0-2])-)?(0?[1-9]|[12][0-9]|3[01])$/; | ||
/** | ||
* Formats the current date in a custom format. | ||
* For example: 08:00 | ||
*/ | ||
function getTimeString() { | ||
var date = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Date(); | ||
var hours = addLeadingZero(date.getHours()); | ||
var minutes = addLeadingZero(date.getMinutes()); | ||
return hours + ':' + minutes; | ||
} | ||
/** | ||
* For example: 2 Feb 2015 | ||
*/ | ||
export function getDateString() { | ||
var date = new Date(); | ||
function getDateString() { | ||
var date = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Date(); | ||
var year = date.getFullYear(); | ||
var monthName = date.toLocaleString('en-US', { month: 'short' }); | ||
var month = date.toLocaleString('en-US', { month: 'short' }); | ||
var day = date.getDate(); | ||
return `${day} ${monthName} ${year}`; | ||
return day + ' ' + month + ' ' + year; | ||
} | ||
/** | ||
* For example: 02.02.2015 | ||
*/ | ||
function getDateString2() { | ||
var date = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Date(); | ||
var year = date.getFullYear(); | ||
var month = addLeadingZero(date.getMonth() + 1); | ||
var day = addLeadingZero(date.getDate()); | ||
return day + '.' + month + '.' + year; | ||
} | ||
/** | ||
* For example: Feb | ||
*/ | ||
function getShortMonthName() { | ||
var date = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Date(); | ||
return date.toLocaleString('en-US', { month: 'short' }); | ||
} | ||
/** | ||
* Formats the current date as per ISO 8601 | ||
* For example: 2015-02-05 | ||
*/ | ||
export function getIsoDateString() { | ||
var date = new Date(); | ||
function getIsoDateString() { | ||
var date = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Date(); | ||
var year = date.getFullYear(); | ||
@@ -24,11 +74,38 @@ var month = addLeadingZero(date.getMonth() + 1); | ||
return `${year}-${month}-${day}`; | ||
return year + '-' + month + '-' + day; | ||
} | ||
/** | ||
* Add a leading zero and convert to String if the number is | ||
* Parse a date string that is more or less formatted like ISO 8601. | ||
* | ||
* But actually it doesn't adhere to the standard at all, because it allows | ||
* the omission of the year and the month. | ||
* | ||
* It also doesn't check if the month and day are within reasonable bounds. | ||
*/ | ||
function parseIsoDateString(str) { | ||
var result = isoDateRegex.exec(str); | ||
if (!result) { | ||
throw new Error('Could not parse the date'); | ||
} | ||
var date = new Date(); | ||
if (result[1] != null) { | ||
date.setFullYear(Number(result[1])); | ||
} | ||
if (result[2] != null) { | ||
date.setMonth(Number(result[2]) - 1); | ||
} | ||
if (result[3] != null) { | ||
date.setDate(Number(result[3])); | ||
} | ||
return date; | ||
} | ||
/** | ||
* Add a leading zero and convert to string if the number is | ||
* smaller than 10 | ||
*/ | ||
function addLeadingZero(number) { | ||
var str = number.toString(); | ||
var str = String(number); | ||
if (str.length < 2) { | ||
@@ -49,3 +126,3 @@ str = '0' + str; | ||
*/ | ||
export function throttle(func, wait, options) { | ||
function throttle(fn, wait, options) { | ||
var context, args, result; | ||
@@ -55,9 +132,9 @@ var timeout = null; | ||
if (!options) options = {}; | ||
var later = function() { | ||
var later = function later() { | ||
previous = options.leading === false ? 0 : Date.now(); | ||
timeout = null; | ||
result = func.apply(context, args); | ||
result = fn.apply(context, args); | ||
if (!timeout) context = args = null; | ||
}; | ||
return function() { | ||
return function () { | ||
var now = Date.now(); | ||
@@ -74,3 +151,3 @@ if (!previous && options.leading === false) previous = now; | ||
previous = now; | ||
result = func.apply(context, args); | ||
result = fn.apply(context, args); | ||
if (!timeout) context = args = null; | ||
@@ -84,3 +161,2 @@ } else if (!timeout && options.trailing !== false) { | ||
/** | ||
@@ -90,4 +166,6 @@ * Returns a function, that, as long as it continues to be invoked, will not | ||
* N milliseconds. | ||
* | ||
* Optionally it groups by the nth parameter (specified by paramIndex) | ||
*/ | ||
export function debounce(fn, wait, hash) { | ||
function debounce(fn, wait, paramIndex) { | ||
var timeouts = {}; | ||
@@ -111,9 +189,9 @@ | ||
// Called from the outside instead of the original function | ||
return function() { | ||
return function () { | ||
// Hash this call | ||
var key; | ||
if (typeof hash === 'number') { | ||
key = arguments[hash]; | ||
} else if (typeof hash === 'function') { | ||
key = hash.apply(undefined, arguments); | ||
if (typeof paramIndex === 'number') { | ||
key = arguments[paramIndex]; | ||
} else if (typeof paramIndex === 'function') { | ||
key = paramIndex.apply(undefined, arguments); | ||
} else { | ||
@@ -120,0 +198,0 @@ key = 'all'; |
{ | ||
"name": "date-tool", | ||
"version": "1.0.3", | ||
"description": "Useful functions for handling Dates", | ||
"keywords": [ "date" ], | ||
"version": "2.1.0", | ||
"description": "Useful functions for Dates", | ||
"keywords": [ | ||
"date" | ||
], | ||
"author": "Jannes Meyer <jannes.meyer@gmail.com>", | ||
"license": "LGPLv3", | ||
"license": "LGPL-3.0", | ||
"repository": "JannesMeyer/date-tool", | ||
"main": "Date.js", | ||
"types": "Date.ts", | ||
"scripts": { | ||
"prepublish": "make", | ||
"test": "make test" | ||
"prepare": "tsc", | ||
"start": "tsc -w", | ||
"test": "tsc && jasmine", | ||
"clean": "rm -f *.log *.js spec/*.js" | ||
}, | ||
"main": "date-tool.es5.js", | ||
"devDependencies": { | ||
"babel": "x", | ||
"jasmine": "^2.2.1" | ||
"dependencies": { | ||
"@types/jasmine": "^3.5.14", | ||
"jasmine": "^3.6.1", | ||
"typescript": "^4.0.3" | ||
} | ||
} |
@@ -7,14 +7,14 @@ # date-tool | ||
~~~bash | ||
npm install date-tool | ||
~~~ | ||
[](https://www.npmjs.com/package/date-tool) | ||
Import the parts you need | ||
Import the parts you need: | ||
~~~js | ||
```js | ||
import { getDateString } from 'date-tool'; | ||
~~~ | ||
``` | ||
## getDateString | ||
## Functions | ||
### getDateString | ||
getDateString() → String | ||
@@ -24,3 +24,3 @@ | ||
## getIsoDateString | ||
### getIsoDateString | ||
@@ -31,8 +31,30 @@ getIsoDateString() → String | ||
## throttle | ||
### throttle | ||
throttle(func, wait[, options]) → Function | ||
## debounce | ||
### debounce | ||
debounce(fn, wait[, hash]) → Function | ||
debounce(fn, wait[, hash]) → Function | ||
## Contributing | ||
Download source and compile: | ||
```sh | ||
git clone git@github.com:JannesMeyer/date-tool.git | ||
yarn | ||
``` | ||
Compile and watch for file changes: | ||
```sh | ||
yarn start | ||
``` | ||
Run tests: | ||
```sh | ||
yarn test | ||
``` |
Sorry, the diff of this file is not supported yet
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
12483
0
353
58
3
7
1
+ Added@types/jasmine@^3.5.14
+ Addedjasmine@^3.6.1
+ Addedtypescript@^4.0.3
+ Added@types/jasmine@3.10.18(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedjasmine@3.99.0(transitive)
+ Addedjasmine-core@3.99.1(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedtypescript@4.9.5(transitive)
+ Addedwrappy@1.0.2(transitive)