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

@internetarchive/search-service

Package Overview
Dependencies
Maintainers
13
Versions
157
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@internetarchive/search-service - npm Package Compare versions

Comparing version 0.1.1-alpha.2 to 0.1.1

dist/src/models/metadata-fields/field-parser-interface.d.ts

16

dist/index.d.ts

@@ -6,10 +6,10 @@ export { Metadata } from './src/models/metadata';

export { SpeechMusicASREntry } from './src/models/speech-music-asr-entry';
export { DateField } from './src/models/metadata-fields/field-types/date';
export { NumberField } from './src/models/metadata-fields/field-types/number';
export { StringField } from './src/models/metadata-fields/field-types/string';
export { BooleanField } from './src/models/metadata-fields/field-types/boolean';
export { ByteField } from './src/models/metadata-fields/field-types/byte';
export { DurationField } from './src/models/metadata-fields/field-types/duration';
export { PageProgressionField } from './src/models/metadata-fields/field-types/page-progression';
export { MediaTypeField } from './src/models/metadata-fields/field-types/mediatype';
export { DateField, DateParser, } from './src/models/metadata-fields/field-types/date';
export { NumberField, NumberParser, } from './src/models/metadata-fields/field-types/number';
export { StringField, StringParser, } from './src/models/metadata-fields/field-types/string';
export { BooleanField, BooleanParser, } from './src/models/metadata-fields/field-types/boolean';
export { Byte, ByteField, ByteParser, } from './src/models/metadata-fields/field-types/byte';
export { Duration, DurationField, DurationParser, } from './src/models/metadata-fields/field-types/duration';
export { PageProgressionField, PageProgressionParser, PageProgression, } from './src/models/metadata-fields/field-types/page-progression';
export { MediaTypeField, MediaTypeParser, MediaType, } from './src/models/metadata-fields/field-types/mediatype';
export { MetadataField } from './src/models/metadata-fields/metadata-field';

@@ -16,0 +16,0 @@ export { MetadataResponse } from './src/responses/metadata/metadata-response';

export { Metadata } from './src/models/metadata';
export { File } from './src/models/file';
export { Review } from './src/models/review';
export { DateField } from './src/models/metadata-fields/field-types/date';
export { NumberField } from './src/models/metadata-fields/field-types/number';
export { StringField } from './src/models/metadata-fields/field-types/string';
export { BooleanField } from './src/models/metadata-fields/field-types/boolean';
export { ByteField } from './src/models/metadata-fields/field-types/byte';
export { DurationField } from './src/models/metadata-fields/field-types/duration';
export { PageProgressionField } from './src/models/metadata-fields/field-types/page-progression';
export { MediaTypeField } from './src/models/metadata-fields/field-types/mediatype';
export { DateField, DateParser, } from './src/models/metadata-fields/field-types/date';
export { NumberField, NumberParser, } from './src/models/metadata-fields/field-types/number';
export { StringField, StringParser, } from './src/models/metadata-fields/field-types/string';
export { BooleanField, BooleanParser, } from './src/models/metadata-fields/field-types/boolean';
export { ByteField, ByteParser, } from './src/models/metadata-fields/field-types/byte';
export { DurationField, DurationParser, } from './src/models/metadata-fields/field-types/duration';
export { PageProgressionField, PageProgressionParser, PageProgression, } from './src/models/metadata-fields/field-types/page-progression';
export { MediaTypeField, MediaTypeParser, MediaType, } from './src/models/metadata-fields/field-types/mediatype';
export { MetadataField } from './src/models/metadata-fields/metadata-field';

@@ -13,0 +13,0 @@ export { MetadataResponse } from './src/responses/metadata/metadata-response';

@@ -1,2 +0,3 @@

import { Byte, Duration } from '@internetarchive/field-parsers';
import type { Byte } from './metadata-fields/field-types/byte';
import type { Duration } from './metadata-fields/field-types/duration';
/**

@@ -3,0 +4,0 @@ * This represents an Internet Archive File

@@ -1,2 +0,4 @@

import { ByteParser, DurationParser, NumberParser, } from '@internetarchive/field-parsers';
import { ByteParser } from './metadata-fields/field-types/byte';
import { DurationParser } from './metadata-fields/field-types/duration';
import { NumberParser } from './metadata-fields/field-types/number';
/**

@@ -3,0 +5,0 @@ * This represents an Internet Archive File

@@ -1,5 +0,9 @@

import { BooleanParser } from '@internetarchive/field-parsers';
import { FieldParserInterface, FieldParserRawValue } from '../field-parser-interface';
import { MetadataField, MetadataRawValue } from '../metadata-field';
export declare class BooleanParser implements FieldParserInterface<boolean> {
static shared: BooleanParser;
parseValue(rawValue: FieldParserRawValue): boolean;
}
export declare class BooleanField extends MetadataField<boolean, BooleanParser> {
constructor(rawValue: MetadataRawValue);
}

@@ -1,3 +0,14 @@

import { BooleanParser } from '@internetarchive/field-parsers';
import { MetadataField } from '../metadata-field';
export class BooleanParser {
parseValue(rawValue) {
if (typeof rawValue === 'string' &&
(rawValue === 'false' || rawValue === '0')) {
return false;
}
return Boolean(rawValue);
}
}
// use a shared static instance for performance instead of
// instantiating a new instance for every use
BooleanParser.shared = new BooleanParser();
export class BooleanField extends MetadataField {

@@ -4,0 +15,0 @@ constructor(rawValue) {

@@ -1,4 +0,20 @@

import { Byte, ByteParser } from '@internetarchive/field-parsers';
import { FieldParserInterface, FieldParserRawValue } from '../field-parser-interface';
import { MetadataField, MetadataRawValue } from '../metadata-field';
/**
* A Byte is a unit-specific `number`, in bytes.
*/
export declare type Byte = number;
/**
* The ByteParser is a unit-specific NumberParser
* that returns a value in bytes
*
* @export
* @class ByteParser
* @implements {FieldParserInterface<Byte>}
*/
export declare class ByteParser implements FieldParserInterface<Byte> {
static shared: ByteParser;
parseValue(rawValue: FieldParserRawValue): Byte | undefined;
}
/**
* ByteField is a unit-specific number field that

@@ -5,0 +21,0 @@ * returns a value in bytes

@@ -1,4 +0,21 @@

import { ByteParser } from '@internetarchive/field-parsers';
import { MetadataField } from '../metadata-field';
import { NumberParser } from './number';
/**
* The ByteParser is a unit-specific NumberParser
* that returns a value in bytes
*
* @export
* @class ByteParser
* @implements {FieldParserInterface<Byte>}
*/
export class ByteParser {
parseValue(rawValue) {
const parser = NumberParser.shared;
return parser.parseValue(rawValue);
}
}
// use a shared static instance for performance instead of
// instantiating a new instance for every use
ByteParser.shared = new ByteParser();
/**
* ByteField is a unit-specific number field that

@@ -5,0 +22,0 @@ * returns a value in bytes

@@ -1,5 +0,11 @@

import { DateParser } from '@internetarchive/field-parsers';
import { FieldParserInterface } from '../field-parser-interface';
import { MetadataField, MetadataRawValue } from '../metadata-field';
export declare class DateParser implements FieldParserInterface<Date> {
static shared: DateParser;
parseValue(rawValue: string): Date | undefined;
private parseBracketDate;
private parseJSDate;
}
export declare class DateField extends MetadataField<Date, DateParser> {
constructor(rawValue: MetadataRawValue);
}

@@ -1,3 +0,44 @@

import { DateParser } from '@internetarchive/field-parsers';
import { MetadataField } from '../metadata-field';
export class DateParser {
parseValue(rawValue) {
// try different date parsing
return this.parseJSDate(rawValue) || this.parseBracketDate(rawValue);
}
// handles "[yyyy]" format
parseBracketDate(rawValue) {
const yearMatch = rawValue.match(/\[([0-9]{4})\]/);
if (!yearMatch || yearMatch.length < 2) {
return undefined;
}
return this.parseJSDate(yearMatch[1]);
}
parseJSDate(rawValue) {
if (typeof rawValue !== 'string')
return undefined;
// fix for Safari not supporting `yyyy-mm-dd HH:MM:SS` format, insert a `T` into the space
if (rawValue.match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}\s{1}[0-9]{2}:[0-9]{2}:[0-9]{2}$/)) {
rawValue = rawValue.replace(' ', 'T');
}
const parsed = Date.parse(rawValue);
if (Number.isNaN(parsed)) {
return undefined;
}
let date = new Date(rawValue);
// the `Date(string)` constructor parses some strings as GMT and some in the local timezone
// this attempts to detect cases that get parsed as GMT and adjusts accordingly
const dateWithTimeZone = rawValue.indexOf('Z') > -1 || // ISO8601 with GMT timezone
rawValue.indexOf('+') > -1 || // ISO8601 with positive timezone offset
rawValue.match(/^[0-9]{4}$/) || // just the year, ie `2020`
rawValue.match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/) || // YYYY-MM-DD format
rawValue.match(/^.*?-[0-9]{2}:[0-9]{2}$/) || // `YYYY-MM-DDTHH:mm:ss-00:00` format
rawValue.match(/^.*?-[0-9]{4}$/); // `YYYY-MM-DDTHH:mm:ss-0000` format
if (dateWithTimeZone) {
date = new Date(date.getTime() + date.getTimezoneOffset() * 1000 * 60);
}
return date;
}
}
// use a shared static instance for performance instead of
// instantiating a new instance for every use
DateParser.shared = new DateParser();
export class DateField extends MetadataField {

@@ -4,0 +45,0 @@ constructor(rawValue) {

@@ -1,4 +0,17 @@

import { Duration, DurationParser } from '@internetarchive/field-parsers';
import { FieldParserInterface, FieldParserRawValue } from '../field-parser-interface';
import { MetadataField, MetadataRawValue } from '../metadata-field';
/**
* Duration is a number in seconds
*/
export declare type Duration = number;
/**
* Parses duration format to a `Duration` (number of seconds with decimal)
*
* Can parse hh:mm:ss.ms, hh:mm:ss, mm:ss, mm:ss.ms, and s.ms formats
*/
export declare class DurationParser implements FieldParserInterface<Duration> {
static shared: DurationParser;
parseValue(rawValue: FieldParserRawValue): Duration | undefined;
}
/**
* The DurationField parses different duration formats

@@ -5,0 +18,0 @@ * and returns a `Duration`, which is a number in seconds

@@ -1,4 +0,32 @@

import { DurationParser } from '@internetarchive/field-parsers';
import { MetadataField } from '../metadata-field';
/**
* Parses duration format to a `Duration` (number of seconds with decimal)
*
* Can parse hh:mm:ss.ms, hh:mm:ss, mm:ss, mm:ss.ms, and s.ms formats
*/
export class DurationParser {
parseValue(rawValue) {
if (typeof rawValue === 'number')
return rawValue;
if (typeof rawValue === 'boolean')
return undefined;
const componentArray = rawValue.split(':');
const componentCount = componentArray.length;
const seconds = componentArray
.map((element, index) => {
const componentValue = parseFloat(element);
if (Number.isNaN(componentValue))
return 0;
const exponent = componentCount - 1 - index;
const multiplier = 60 ** exponent;
return componentValue * Math.floor(multiplier);
})
.reduce((a, b) => a + b, 0);
return seconds;
}
}
// use a shared static instance for performance instead of
// instantiating a new instance for every use
DurationParser.shared = new DurationParser();
/**
* The DurationField parses different duration formats

@@ -5,0 +33,0 @@ * and returns a `Duration`, which is a number in seconds

@@ -1,5 +0,20 @@

import { MediaType, MediaTypeParser } from '@internetarchive/field-parsers';
import { FieldParserInterface, FieldParserRawValue } from '../field-parser-interface';
import { MetadataField, MetadataRawValue } from '../metadata-field';
export declare enum MediaType {
Audio = "audio",
Collection = "collection",
Data = "data",
Etree = "etree",
Image = "image",
Movies = "movies",
Software = "software",
Texts = "texts",
Web = "web"
}
export declare class MediaTypeParser implements FieldParserInterface<MediaType> {
static shared: MediaTypeParser;
parseValue(rawValue: FieldParserRawValue): MediaType | undefined;
}
export declare class MediaTypeField extends MetadataField<MediaType, MediaTypeParser> {
constructor(rawValue: MetadataRawValue);
}

@@ -1,3 +0,43 @@

import { MediaTypeParser } from '@internetarchive/field-parsers';
import { MetadataField } from '../metadata-field';
export var MediaType;
(function (MediaType) {
MediaType["Audio"] = "audio";
MediaType["Collection"] = "collection";
MediaType["Data"] = "data";
MediaType["Etree"] = "etree";
MediaType["Image"] = "image";
MediaType["Movies"] = "movies";
MediaType["Software"] = "software";
MediaType["Texts"] = "texts";
MediaType["Web"] = "web";
})(MediaType || (MediaType = {}));
export class MediaTypeParser {
parseValue(rawValue) {
switch (rawValue) {
case 'audio':
return MediaType.Audio;
case 'collection':
return MediaType.Collection;
case 'data':
return MediaType.Data;
case 'etree':
return MediaType.Etree;
case 'image':
return MediaType.Image;
case 'movies':
return MediaType.Movies;
case 'software':
return MediaType.Software;
case 'texts':
return MediaType.Texts;
case 'web':
return MediaType.Web;
default:
return undefined;
}
}
}
// use a shared static instance for performance instead of
// instantiating a new instance for every use
MediaTypeParser.shared = new MediaTypeParser();
export class MediaTypeField extends MetadataField {

@@ -4,0 +44,0 @@ constructor(rawValue) {

@@ -1,5 +0,9 @@

import { NumberParser } from '@internetarchive/field-parsers';
import { FieldParserInterface, FieldParserRawValue } from '../field-parser-interface';
import { MetadataField, MetadataRawValue } from '../metadata-field';
export declare class NumberParser implements FieldParserInterface<number> {
static shared: NumberParser;
parseValue(rawValue: FieldParserRawValue): number | undefined;
}
export declare class NumberField extends MetadataField<number, NumberParser> {
constructor(rawValue: MetadataRawValue);
}

@@ -1,3 +0,18 @@

import { NumberParser } from '@internetarchive/field-parsers';
import { MetadataField } from '../metadata-field';
export class NumberParser {
parseValue(rawValue) {
if (typeof rawValue === 'number')
return rawValue;
if (typeof rawValue === 'boolean')
return undefined;
const value = parseFloat(rawValue);
if (Number.isNaN(value)) {
return undefined;
}
return value;
}
}
// use a shared static instance for performance instead of
// instantiating a new instance for every use
NumberParser.shared = new NumberParser();
export class NumberField extends MetadataField {

@@ -4,0 +19,0 @@ constructor(rawValue) {

@@ -1,5 +0,13 @@

import { PageProgression, PageProgressionParser } from '@internetarchive/field-parsers';
import { FieldParserInterface, FieldParserRawValue } from '../field-parser-interface';
import { MetadataField, MetadataRawValue } from '../metadata-field';
export declare enum PageProgression {
RightToLeft = "rl",
LeftToRight = "lr"
}
export declare class PageProgressionParser implements FieldParserInterface<PageProgression> {
static shared: PageProgressionParser;
parseValue(rawValue: FieldParserRawValue): PageProgression | undefined;
}
export declare class PageProgressionField extends MetadataField<PageProgression, PageProgressionParser> {
constructor(rawValue: MetadataRawValue);
}

@@ -1,3 +0,24 @@

import { PageProgressionParser, } from '@internetarchive/field-parsers';
import { MetadataField } from '../metadata-field';
export var PageProgression;
(function (PageProgression) {
PageProgression["RightToLeft"] = "rl";
PageProgression["LeftToRight"] = "lr";
})(PageProgression || (PageProgression = {}));
export class PageProgressionParser {
parseValue(rawValue) {
if (typeof rawValue !== 'string')
return undefined;
switch (rawValue) {
case 'rl':
return PageProgression.RightToLeft;
case 'lr':
return PageProgression.LeftToRight;
default:
return undefined;
}
}
}
// use a shared static instance for performance instead of
// instantiating a new instance for every use
PageProgressionParser.shared = new PageProgressionParser();
export class PageProgressionField extends MetadataField {

@@ -4,0 +25,0 @@ constructor(rawValue) {

@@ -1,5 +0,9 @@

import { StringParser } from '@internetarchive/field-parsers';
import { FieldParserInterface, FieldParserRawValue } from '../field-parser-interface';
import { MetadataField, MetadataRawValue } from '../metadata-field';
export declare class StringParser implements FieldParserInterface<string> {
static shared: StringParser;
parseValue(rawValue: FieldParserRawValue): string;
}
export declare class StringField extends MetadataField<string, StringParser> {
constructor(rawValue: MetadataRawValue);
}

@@ -1,3 +0,10 @@

import { StringParser } from '@internetarchive/field-parsers';
import { MetadataField } from '../metadata-field';
export class StringParser {
parseValue(rawValue) {
return String(rawValue);
}
}
// use a shared static instance for performance instead of
// instantiating a new instance for every use
StringParser.shared = new StringParser();
export class StringField extends MetadataField {

@@ -4,0 +11,0 @@ constructor(rawValue) {

@@ -1,2 +0,2 @@

import { FieldParserInterface } from '@internetarchive/field-parsers';
import { FieldParserInterface } from './field-parser-interface';
export declare type MetadataRawValue = string | string[] | number | boolean;

@@ -3,0 +3,0 @@ /**

/* eslint-disable @typescript-eslint/no-explicit-any */
import { DateParser } from '@internetarchive/field-parsers';
import { DateParser } from './metadata-fields/field-types/date';
export class Review {

@@ -4,0 +4,0 @@ constructor(json) {

@@ -7,17 +7,45 @@ export { Metadata } from './src/models/metadata';

export { DateField } from './src/models/metadata-fields/field-types/date';
export {
DateField,
DateParser,
} from './src/models/metadata-fields/field-types/date';
export { NumberField } from './src/models/metadata-fields/field-types/number';
export {
NumberField,
NumberParser,
} from './src/models/metadata-fields/field-types/number';
export { StringField } from './src/models/metadata-fields/field-types/string';
export {
StringField,
StringParser,
} from './src/models/metadata-fields/field-types/string';
export { BooleanField } from './src/models/metadata-fields/field-types/boolean';
export {
BooleanField,
BooleanParser,
} from './src/models/metadata-fields/field-types/boolean';
export { ByteField } from './src/models/metadata-fields/field-types/byte';
export {
Byte,
ByteField,
ByteParser,
} from './src/models/metadata-fields/field-types/byte';
export { DurationField } from './src/models/metadata-fields/field-types/duration';
export {
Duration,
DurationField,
DurationParser,
} from './src/models/metadata-fields/field-types/duration';
export { PageProgressionField } from './src/models/metadata-fields/field-types/page-progression';
export {
PageProgressionField,
PageProgressionParser,
PageProgression,
} from './src/models/metadata-fields/field-types/page-progression';
export { MediaTypeField } from './src/models/metadata-fields/field-types/mediatype';
export {
MediaTypeField,
MediaTypeParser,
MediaType,
} from './src/models/metadata-fields/field-types/mediatype';

@@ -24,0 +52,0 @@ export { MetadataField } from './src/models/metadata-fields/metadata-field';

{
"name": "@internetarchive/search-service",
"version": "0.1.1-alpha.2",
"version": "0.1.1",
"description": "A search service for the Internet Archive",

@@ -72,5 +72,4 @@ "license": "AGPL-3.0-only",

"dependencies": {
"@internetarchive/field-parsers": "^0.0.1-alpha.3",
"@internetarchive/result-type": "^0.0.1"
}
}

@@ -1,8 +0,6 @@

import {
Byte,
ByteParser,
Duration,
DurationParser,
NumberParser,
} from '@internetarchive/field-parsers';
import type { Byte } from './metadata-fields/field-types/byte';
import type { Duration } from './metadata-fields/field-types/duration';
import { ByteParser } from './metadata-fields/field-types/byte';
import { DurationParser } from './metadata-fields/field-types/duration';
import { NumberParser } from './metadata-fields/field-types/number';

@@ -9,0 +7,0 @@ /**

@@ -1,4 +0,23 @@

import { BooleanParser } from '@internetarchive/field-parsers';
import {
FieldParserInterface,
FieldParserRawValue,
} from '../field-parser-interface';
import { MetadataField, MetadataRawValue } from '../metadata-field';
export class BooleanParser implements FieldParserInterface<boolean> {
// use a shared static instance for performance instead of
// instantiating a new instance for every use
static shared = new BooleanParser();
parseValue(rawValue: FieldParserRawValue): boolean {
if (
typeof rawValue === 'string' &&
(rawValue === 'false' || rawValue === '0')
) {
return false;
}
return Boolean(rawValue);
}
}
export class BooleanField extends MetadataField<boolean, BooleanParser> {

@@ -5,0 +24,0 @@ constructor(rawValue: MetadataRawValue) {

@@ -1,5 +0,33 @@

import { Byte, ByteParser } from '@internetarchive/field-parsers';
import {
FieldParserInterface,
FieldParserRawValue,
} from '../field-parser-interface';
import { MetadataField, MetadataRawValue } from '../metadata-field';
import { NumberParser } from './number';
/**
* A Byte is a unit-specific `number`, in bytes.
*/
export type Byte = number;
/**
* The ByteParser is a unit-specific NumberParser
* that returns a value in bytes
*
* @export
* @class ByteParser
* @implements {FieldParserInterface<Byte>}
*/
export class ByteParser implements FieldParserInterface<Byte> {
// use a shared static instance for performance instead of
// instantiating a new instance for every use
static shared = new ByteParser();
parseValue(rawValue: FieldParserRawValue): Byte | undefined {
const parser = NumberParser.shared;
return parser.parseValue(rawValue);
}
}
/**
* ByteField is a unit-specific number field that

@@ -6,0 +34,0 @@ * returns a value in bytes

@@ -1,4 +0,59 @@

import { DateParser } from '@internetarchive/field-parsers';
import {
FieldParserInterface,
FieldParserRawValue,
} from '../field-parser-interface';
import { MetadataField, MetadataRawValue } from '../metadata-field';
export class DateParser implements FieldParserInterface<Date> {
// use a shared static instance for performance instead of
// instantiating a new instance for every use
static shared = new DateParser();
parseValue(rawValue: string): Date | undefined {
// try different date parsing
return this.parseJSDate(rawValue) || this.parseBracketDate(rawValue);
}
// handles "[yyyy]" format
private parseBracketDate(rawValue: string): Date | undefined {
const yearMatch = rawValue.match(/\[([0-9]{4})\]/);
if (!yearMatch || yearMatch.length < 2) {
return undefined;
}
return this.parseJSDate(yearMatch[1]);
}
private parseJSDate(rawValue: FieldParserRawValue): Date | undefined {
if (typeof rawValue !== 'string') return undefined;
// fix for Safari not supporting `yyyy-mm-dd HH:MM:SS` format, insert a `T` into the space
if (
rawValue.match(
/^[0-9]{4}-[0-9]{2}-[0-9]{2}\s{1}[0-9]{2}:[0-9]{2}:[0-9]{2}$/
)
) {
rawValue = rawValue.replace(' ', 'T');
}
const parsed = Date.parse(rawValue);
if (Number.isNaN(parsed)) {
return undefined;
}
let date = new Date(rawValue);
// the `Date(string)` constructor parses some strings as GMT and some in the local timezone
// this attempts to detect cases that get parsed as GMT and adjusts accordingly
const dateWithTimeZone =
rawValue.indexOf('Z') > -1 || // ISO8601 with GMT timezone
rawValue.indexOf('+') > -1 || // ISO8601 with positive timezone offset
rawValue.match(/^[0-9]{4}$/) || // just the year, ie `2020`
rawValue.match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/) || // YYYY-MM-DD format
rawValue.match(/^.*?-[0-9]{2}:[0-9]{2}$/) || // `YYYY-MM-DDTHH:mm:ss-00:00` format
rawValue.match(/^.*?-[0-9]{4}$/); // `YYYY-MM-DDTHH:mm:ss-0000` format
if (dateWithTimeZone) {
date = new Date(date.getTime() + date.getTimezoneOffset() * 1000 * 60);
}
return date;
}
}
export class DateField extends MetadataField<Date, DateParser> {

@@ -5,0 +60,0 @@ constructor(rawValue: MetadataRawValue) {

@@ -1,5 +0,43 @@

import { Duration, DurationParser } from '@internetarchive/field-parsers';
import {
FieldParserInterface,
FieldParserRawValue,
} from '../field-parser-interface';
import { MetadataField, MetadataRawValue } from '../metadata-field';
/**
* Duration is a number in seconds
*/
export type Duration = number;
/**
* Parses duration format to a `Duration` (number of seconds with decimal)
*
* Can parse hh:mm:ss.ms, hh:mm:ss, mm:ss, mm:ss.ms, and s.ms formats
*/
export class DurationParser implements FieldParserInterface<Duration> {
// use a shared static instance for performance instead of
// instantiating a new instance for every use
static shared = new DurationParser();
parseValue(rawValue: FieldParserRawValue): Duration | undefined {
if (typeof rawValue === 'number') return rawValue;
if (typeof rawValue === 'boolean') return undefined;
const componentArray: string[] = rawValue.split(':');
const componentCount: number = componentArray.length;
const seconds: number | undefined = componentArray
.map((element: string, index: number) => {
const componentValue: number = parseFloat(element);
if (Number.isNaN(componentValue)) return 0;
const exponent: number = componentCount - 1 - index;
const multiplier: number = 60 ** exponent;
return componentValue * Math.floor(multiplier);
})
.reduce((a, b) => a + b, 0);
return seconds;
}
}
/**
* The DurationField parses different duration formats

@@ -6,0 +44,0 @@ * and returns a `Duration`, which is a number in seconds

@@ -1,4 +0,50 @@

import { MediaType, MediaTypeParser } from '@internetarchive/field-parsers';
import {
FieldParserInterface,
FieldParserRawValue,
} from '../field-parser-interface';
import { MetadataField, MetadataRawValue } from '../metadata-field';
export enum MediaType {
Audio = 'audio',
Collection = 'collection',
Data = 'data',
Etree = 'etree',
Image = 'image',
Movies = 'movies',
Software = 'software',
Texts = 'texts',
Web = 'web',
}
export class MediaTypeParser implements FieldParserInterface<MediaType> {
// use a shared static instance for performance instead of
// instantiating a new instance for every use
static shared = new MediaTypeParser();
parseValue(rawValue: FieldParserRawValue): MediaType | undefined {
switch (rawValue) {
case 'audio':
return MediaType.Audio;
case 'collection':
return MediaType.Collection;
case 'data':
return MediaType.Data;
case 'etree':
return MediaType.Etree;
case 'image':
return MediaType.Image;
case 'movies':
return MediaType.Movies;
case 'software':
return MediaType.Software;
case 'texts':
return MediaType.Texts;
case 'web':
return MediaType.Web;
default:
return undefined;
}
}
}
export class MediaTypeField extends MetadataField<MediaType, MediaTypeParser> {

@@ -5,0 +51,0 @@ constructor(rawValue: MetadataRawValue) {

@@ -1,4 +0,24 @@

import { NumberParser } from '@internetarchive/field-parsers';
import {
FieldParserInterface,
FieldParserRawValue,
} from '../field-parser-interface';
import { MetadataField, MetadataRawValue } from '../metadata-field';
export class NumberParser implements FieldParserInterface<number> {
// use a shared static instance for performance instead of
// instantiating a new instance for every use
static shared = new NumberParser();
parseValue(rawValue: FieldParserRawValue): number | undefined {
if (typeof rawValue === 'number') return rawValue;
if (typeof rawValue === 'boolean') return undefined;
const value = parseFloat(rawValue);
if (Number.isNaN(value)) {
return undefined;
}
return value;
}
}
export class NumberField extends MetadataField<number, NumberParser> {

@@ -5,0 +25,0 @@ constructor(rawValue: MetadataRawValue) {

import {
PageProgression,
PageProgressionParser,
} from '@internetarchive/field-parsers';
FieldParserInterface,
FieldParserRawValue,
} from '../field-parser-interface';
import { MetadataField, MetadataRawValue } from '../metadata-field';
export enum PageProgression {
RightToLeft = 'rl',
LeftToRight = 'lr',
}
export class PageProgressionParser
implements FieldParserInterface<PageProgression> {
// use a shared static instance for performance instead of
// instantiating a new instance for every use
static shared = new PageProgressionParser();
parseValue(rawValue: FieldParserRawValue): PageProgression | undefined {
if (typeof rawValue !== 'string') return undefined;
switch (rawValue) {
case 'rl':
return PageProgression.RightToLeft;
case 'lr':
return PageProgression.LeftToRight;
default:
return undefined;
}
}
}
export class PageProgressionField extends MetadataField<

@@ -8,0 +33,0 @@ PageProgression,

@@ -1,4 +0,17 @@

import { StringParser } from '@internetarchive/field-parsers';
import {
FieldParserInterface,
FieldParserRawValue,
} from '../field-parser-interface';
import { MetadataField, MetadataRawValue } from '../metadata-field';
export class StringParser implements FieldParserInterface<string> {
// use a shared static instance for performance instead of
// instantiating a new instance for every use
static shared = new StringParser();
parseValue(rawValue: FieldParserRawValue): string {
return String(rawValue);
}
}
export class StringField extends MetadataField<string, StringParser> {

@@ -5,0 +18,0 @@ constructor(rawValue: MetadataRawValue) {

import {
FieldParserInterface,
FieldParserRawValue,
} from '@internetarchive/field-parsers';
} from './field-parser-interface';

@@ -6,0 +6,0 @@ export type MetadataRawValue = string | string[] | number | boolean;

/* eslint-disable @typescript-eslint/no-explicit-any */
import { DateParser } from '@internetarchive/field-parsers';
import { DateParser } from './metadata-fields/field-types/date';

@@ -5,0 +5,0 @@ export class Review {

@@ -0,6 +1,7 @@

import { expect } from '@open-wc/testing';
import {
FieldParserInterface,
FieldParserRawValue,
} from '@internetarchive/field-parsers/dist/src/field-parser-interface';
import { expect } from '@open-wc/testing';
} from '../../../src/models/metadata-fields/field-parser-interface';
import { MetadataField } from '../../../src/models/metadata-fields/metadata-field';

@@ -7,0 +8,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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