Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

calculate-age

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

calculate-age - npm Package Compare versions

Comparing version 0.1.0 to 1.0.0

dist/index.d.ts

87

dist/index.js
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var _require = require('./lib'),
validate = _require.validate,
calculate = _require.calculate;
var calculateAge = /*#__PURE__*/function () {
function calculateAge(dateOfBirth) {
var compareDate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
_classCallCheck(this, calculateAge);
this.dateOfBirth = dateOfBirth;
this.compareDate = compareDate ? compareDate : new Date();
}
_createClass(calculateAge, [{
key: "getObject",
value: function getObject() {
validate(this.dateOfBirth, this.compareDate);
return calculate(this.dateOfBirth, this.compareDate);
}
}, {
key: "getString",
value: function getString() {
validate(this.dateOfBirth, this.compareDate);
var diffObj = calculate(this.dateOfBirth, this.compareDate);
var yearString = "".concat(diffObj.years, " ").concat(diffObj.years == 1 ? 'year' : 'years');
var monthString = "".concat(diffObj.months, " ").concat(diffObj.months == 1 ? 'month' : 'months');
var dayString = "".concat(diffObj.days, " ").concat(diffObj.days == 1 ? 'day' : 'days');
if (diffObj.years > 0) {
return diffObj.months > 0 ? "".concat(yearString, " ").concat(monthString) : "".concat(yearString);
} else if (diffObj.months > 0) {
return diffObj.days > 0 ? "".concat(monthString, " ").concat(dayString) : "".concat(monthString);
} else if (diffObj.days > 0) {
return "".concat(dayString);
} else {
return "today";
}
}
}]);
return calculateAge;
}();
var _default = calculateAge;
exports["default"] = _default;
Object.defineProperty(exports, "__esModule", { value: true });
var lib_1 = require("./lib");
var calculateAge = function (dateOfBirth, compareDate) {
var dateOfBirthObj = typeof dateOfBirth === 'string' ? new Date(dateOfBirth) : dateOfBirth;
var compareDateObj = compareDate ? (typeof compareDate === 'string' ? new Date(compareDate) : compareDate) : new Date();
(0, lib_1.validate)(dateOfBirthObj.toISOString(), compareDateObj.toISOString());
return {
getObject: function () { return (0, lib_1.calculate)(dateOfBirthObj.toISOString(), compareDateObj.toISOString()); },
getString: function () {
var diffObj = (0, lib_1.calculate)(dateOfBirthObj.toISOString(), compareDateObj.toISOString());
var yearString = "".concat(diffObj.years, " year").concat(diffObj.years !== 1 ? 's' : '');
var monthString = "".concat(diffObj.months, " month").concat(diffObj.months !== 1 ? 's' : '');
var dayString = "".concat(diffObj.days, " day").concat(diffObj.days !== 1 ? 's' : '');
if (diffObj.years > 0) {
return (diffObj.months > 0) ? "".concat(yearString, " ").concat(monthString) : "".concat(yearString);
}
else if (diffObj.months > 0) {
return (diffObj.days > 0) ? "".concat(monthString, " ").concat(dayString) : "".concat(monthString);
}
else if (diffObj.days > 0) {
return "".concat(dayString);
}
else {
return "today";
}
}
};
};
exports.default = calculateAge;
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculate = exports.validate = void 0;
var dayjs = require('dayjs');
var duration = require('dayjs/plugin/duration');
dayjs.extend(duration);
var isValid = function isValid(dateString) {
return dayjs(dateString).isValid();
var isValid = function (dateString) {
return !isNaN(Date.parse(dateString));
};
var isBefore = function isBefore(birthDate, compareDate) {
var currentDay = dayjs(compareDate);
var birthDay = dayjs(birthDate);
return birthDay.isBefore(currentDay);
var isBefore = function (birthDate, compareDate) {
var currentDay = new Date(compareDate);
var birthDay = new Date(birthDate);
return birthDay <= currentDay;
};
var validate = function validate(birthDate, compareDate) {
if (!isValid(birthDate) && !isValid(compareDate)) {
throw 'Invalid date format';
}
if (!isBefore(birthDate, compareDate)) {
throw 'Invalid date';
}
var validate = function (birthDate, compareDate) {
if (!isValid(birthDate) || !isValid(compareDate)) {
throw new Error('Invalid date format');
}
if (!isBefore(birthDate, compareDate)) {
throw new Error('Invalid date');
}
};
exports.validate = validate;
var calculate = function calculate(birthDate, compareDate) {
var currentDay = dayjs(compareDate);
var birthDay = dayjs(birthDate);
var dateDiff = dayjs.duration(currentDay.diff(birthDay));
return {
'years': dateDiff.years(),
'months': dateDiff.months(),
'days': dateDiff.days()
};
var calculate = function (birthDate, compareDate) {
var currentDay = new Date(compareDate);
var birthDay = new Date(birthDate);
var yearsDiff = currentDay.getFullYear() - birthDay.getFullYear();
var monthsDiff = currentDay.getMonth() - birthDay.getMonth();
var daysDiff = currentDay.getDate() - birthDay.getDate();
var years = yearsDiff;
var months = monthsDiff;
var days = daysDiff;
if (monthsDiff < 0 || (monthsDiff === 0 && daysDiff < 0)) {
years--;
months += 12;
}
if (daysDiff < 0) {
var lastMonthDays = new Date(currentDay.getFullYear(), currentDay.getMonth(), 0).getDate();
months--;
days += lastMonthDays;
}
return {
'years': years,
'months': months,
'days': days
};
};
exports.calculate = calculate;
exports.calculate = calculate;
{
"name": "calculate-age",
"version": "0.1.0",
"version": "1.0.0",
"description": "Calculate age of given date of birth and some other useful function",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"test": "jest",
"build": "babel src -d dist",
"prepare": "npm run build && npm run test"
"build": "tsc",
"test": "jest"
},
"author": "Arvindkumar Badwal <arvindkumar.b@hotmail.com>",
"license": "MIT",
"dependencies": {
"dayjs": "^1.10.4"
},
"devDependencies": {
"@babel/cli": "^7.12.10",
"@babel/core": "^7.12.10",
"@babel/preset-env": "^7.12.11",
"jest": "^26.6.3"
"@types/jest": "^29.5.0",
"jest": "^29.5.0",
"ts-jest": "^29.1.0",
"typescript": "^5.0.4"
},

@@ -26,2 +23,2 @@ "bugs": {

"homepage": "https://github.com/arvindkumarbadwal/calculate-age"
}
}

@@ -15,10 +15,17 @@ # calculate-age

## Using
Function Parameter
```js
calculateAge(dateOfBirth: string | Date, compareDate?: string | Date)
```
Example
```js
import calculateAge from 'calculate-age'
// or const calculateAge = require('calculate-age').default
// OR const calculateAge = require('calculate-age').default
const output = new calculateAge('1988-10-26', '2021-01-26').getObject()
const output = calculateAge('1988-10-26', '2021-01-26').getObject()
// output: { years: 32, months: 3, days: 10 }
const output = new calculateAge('1988-10-26', '2021-01-26').getString()
const output = calculateAge('1988-10-26', '2021-01-26').getString()
// output: 32 years 3 months

@@ -42,3 +49,11 @@ ```

```
## Release Notes - Version 1.0.0
### Features
- Added TypeScript support to the package.
- Changed from class-based to function-based implementation for `calculateAge` method, removing the need for `new` keyword.
- Made `compareDate` optional in `calculateAge` method, defaulting it to today's date.
## Contributing

@@ -45,0 +60,0 @@

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc