Socket
Socket
Sign inDemoInstall

@scoir/date

Package Overview
Dependencies
6
Maintainers
36
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.4-0 to 2.0.4-1

2

dist/is-credible-date.js

@@ -8,5 +8,5 @@ "use strict";

var EMPTY_FORMAT = [undefined, null, '', '0001-01-01', '0001-01-01T00:00:00Z'];
/** @deprecated */
/** @deprecated check null/undefined yourself and if you're using this to check for zero date values, work with your team to make sure the API doesn't return them*/
var _default = exports["default"] = function _default(input) {
return !(0, _includes["default"])(EMPTY_FORMAT, input);
};

@@ -12,3 +12,2 @@ "use strict";

var intervalId;
var baseDate = new Date();
var createInterval = function createInterval() {

@@ -33,8 +32,9 @@ if (!intervalId) {

};
/**
* Provides a time string relative to now that updates every 60 seconds
* @param {string} time - a date string that date-fns intlFormatDistance can accept
* @param {Date} time
*/
var useRelativeTime = function useRelativeTime(time) {
var _useState = (0, _react.useState)(''),
var _useState = (0, _react.useState)(""),
formatStr = _useState[0],

@@ -45,9 +45,7 @@ setFormatStr = _useState[1];

if (!!time) {
var distanceDate = new Date(time);
if (!distanceDate) return '';
setFormatStr((0, _dateFns.intlFormatDistance)(distanceDate, baseDate));
deregister = register(function () {
var date = new Date();
setFormatStr((0, _dateFns.intlFormatDistance)(new Date(time), date));
});
var fn = function fn() {
return setFormatStr((0, _dateFns.intlFormatDistance)(time, new Date()));
};
fn();
deregister = register(fn);
}

@@ -54,0 +52,0 @@ return function () {

@@ -18,3 +18,3 @@ "use strict";

jest.useFakeTimers();
jest.setSystemTime(new Date(2024, 1, 22));
jest.setSystemTime(new Date(2024, 4, 22));
});

@@ -26,6 +26,6 @@ afterAll(function () {

var view = (0, _react2.render)(build({
time: '2024-01-05T19:17:39.875Z'
time: new Date('2024-05-05T19:17:39.875Z')
}));
expect(view.getByTestId('r-time')).toHaveTextContent('3 weeks ago');
jest.setSystemTime(new Date(2024, 1, 29));
expect(view.getByTestId('r-time')).toHaveTextContent('2 weeks ago');
jest.setSystemTime(new Date(2024, 5, 6));
(0, _react2.act)(function () {

@@ -32,0 +32,0 @@ return jest.advanceTimersByTime(60001);

import { includes } from 'lodash'
const EMPTY_FORMAT = [undefined, null, '', '0001-01-01', '0001-01-01T00:00:00Z']
/** @deprecated */
/** @deprecated check null/undefined yourself and if you're using this to check for zero date values, work with your team to make sure the API doesn't return them*/
export default input => {
return !includes(EMPTY_FORMAT, input)
}
{
"name": "@scoir/date",
"version": "2.0.4-0",
"version": "2.0.4-1",
"description": "",

@@ -17,9 +17,11 @@ "author": "@scoir",

"date-fns": "^2.28.0",
"lodash": "~4.17.11"
"lodash": "~4.17.11",
"moment-timezone": "^0.5.34"
},
"dependencies": {
"devDependencies": {
"date-fns": "^2.28.0",
"lodash": "~4.17.11",
"moment-timezone": "^0.5.34"
},
"gitHead": "93379ddec91dd661fea70d196cad1a548ef43558"
"gitHead": "5d66274f0cda15c912a3af81f928378cfbefe65a"
}

@@ -1,51 +0,49 @@

import React, {useState, useEffect} from 'react'
import React, { useState, useEffect } from "react";
import {intlFormatDistance} from 'date-fns'
import { intlFormatDistance } from "date-fns";
const listeners = {}
const listeners = {};
let counter = 0;
let intervalId
const baseDate = new Date()
let intervalId;
const createInterval = () => {
if (!intervalId) {
intervalId = setInterval(() => Object.values(listeners).forEach(listener => listener()), 60 * 1000)
intervalId = setInterval(
() => Object.values(listeners).forEach((listener) => listener()),
60 * 1000
);
}
}
};
const register = callback => {
createInterval()
const myCounter = counter++
listeners[myCounter] = callback
const register = (callback) => {
createInterval();
const myCounter = counter++;
listeners[myCounter] = callback;
return () => {
if (listeners[myCounter]) {
delete listeners[myCounter]
delete listeners[myCounter];
}
}
}
};
};
/**
* Provides a time string relative to now that updates every 60 seconds
* @param {string} time - a date string that date-fns intlFormatDistance can accept
* @param {Date} time
*/
const useRelativeTime = (time) => {
const [formatStr, setFormatStr] = useState('')
const [formatStr, setFormatStr] = useState("");
useEffect(() => {
let deregister
let deregister;
if (!!time) {
const distanceDate = new Date(time)
if (!distanceDate) return ''
setFormatStr(intlFormatDistance(distanceDate, baseDate))
deregister = register(() => {
const date = new Date()
setFormatStr(intlFormatDistance(new Date(time), date))
})
const fn = () => setFormatStr(intlFormatDistance(time, new Date()));
fn();
deregister = register(fn);
}
return () => deregister && deregister()
}, [time])
return () => deregister && deregister();
}, [time]);
return formatStr
}
return formatStr;
};
export default useRelativeTime
export default useRelativeTime;

@@ -13,3 +13,3 @@ import React from 'react'

jest.useFakeTimers();
jest.setSystemTime(new Date(2024, 1, 22));
jest.setSystemTime(new Date(2024, 4, 22));
});

@@ -22,6 +22,6 @@

it('should return a formatted date', () => {
const view = render(build({time: '2024-01-05T19:17:39.875Z'}))
expect(view.getByTestId('r-time')).toHaveTextContent('3 weeks ago')
const view = render(build({time: new Date('2024-05-05T19:17:39.875Z')}))
expect(view.getByTestId('r-time')).toHaveTextContent('2 weeks ago')
jest.setSystemTime(new Date(2024, 1, 29))
jest.setSystemTime(new Date(2024, 5, 6))
act( () => jest.advanceTimersByTime(60001))

@@ -28,0 +28,0 @@ expect(view.getByTestId('r-time')).toHaveTextContent('last month')

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc