
Security News
Python Adopts Standard Lock File Format for Reproducible Installs
Python has adopted a standardized lock file format to improve reproducibility, security, and tool interoperability across the packaging ecosystem.
Parso is a lightweight and performant date parser for terrible date formats, it aims to help you with parsing inconsistently formatted dates.
Details TL;DR
What is not included?
Install with npm:
npm install --save parso
Install with yarn:
yarn add parso
import { parse } from 'parso';
const easter = parse('20180401');
const christmas = parse('2018-12-24');
const newYear = parse('2010.01.01');
Parso has a simple API, it exposes two functions - parse
and parseOrThrow
to parse dateish or date-timeish strings. Neither of them require any default options, but their behaviour can be customized via an optional second settings object. The only difference between them is the latter will throw an error if none of the registered parsers can process the passed in value.
import { parse, parseOrThrow, ParsoParseError } from 'parso';
const validValue = '2023.08-21';
const invalidValue = 'you-will-never-parse-me';
try {
parse(validValue); // returns new Date('2023-08-21T00:00:00Z')
parseOrThrow(validValue); // returns new Date('2023-08-21T00:00:00Z')
parse(invalidValue); // returns null
parseOrThrow(invalidValue); // throws ParsoParseError
} catch (error) {
error instanceof ParsoParseError; // true
}
By default only a sane set of parsers for the ISO 8601-ish formats are included in the default registry. You can register non-default or your custom handlers with the defaulParserRegistry.registerParser
function.
Extra parsers are included for extreme formats, you can import them from 'parso/parsers'. You can read more about the included parsers in their documentation.
Parsers are simple functions which implement the DateParser
type. They must meet the following criteria:
undefined
when failed to parse the recieved valueExample:
/**
* Parses a valid date string.
*/
export const validDateParser: DateParser = (value: string | number): Date | undefined => {
const invalidDate = Number.isNaN(new Date(value).getTime());
return invalidDate ? undefined : new Date(value);
};
You can read more about custom parsers in their documentation.
parse
functionTries to parse the recieved value into a Date
object with the registered parsers, returns null
when the parsing attempt fails.
Possible return values:
Date
instancenull
value when none of the parsers can parse the recieived valuePossible errors:
ParsoInvalidInputError
when the recieved value is not a string
, number
or Date
type.Signature:
parse(value: string | number | Date, parseOptions: ParseOptions): Date | null
parseOrThrow
functionTries to parse the recieved value into a Date
object with the registered parsers, throws an instance of ParsoParseError
error when the parsing attempt fails.
Possible return values:
Date
instancePossible errors:
ParsoInvalidInputError
when the recieved value is not a string
, number
or Date
type.ParsoParseError
when none of the registered parsers can parser the recieved value.Signature:
parseOrThrow(value: string | number | Date, parseOptions: ParseOptions): Date
ParserRegistry
classA ParserRegistry
instance can be used to store parsers. Which later can be passed into the parse functions via the parseOptions.customRegistry
option.
import { ParserRegistry, parse } from 'parso';
import { myCustomParser } from './my-custom-parser';
const customRegistry = new ParserRegistry();
customRegistry.registerParsers(myCustomParser);
parse('2019_08_01', { customRegistry });
Parso exports a default registry instance named defaulParserRegistry
which is used by the parser functions when no custom registry is specified.
DateParser
typeSee the parser documentation for details.
FAQs
Lightweight & performant date parser for terrible date formats.
The npm package parso receives a total of 3 weekly downloads. As such, parso popularity was classified as not popular.
We found that parso demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Python has adopted a standardized lock file format to improve reproducibility, security, and tool interoperability across the packaging ecosystem.
Security News
OpenGrep has restored fingerprint and metavariable support in JSON and SARIF outputs, making static analysis more effective for CI/CD security automation.
Security News
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.