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

wikibase-sdk

Package Overview
Dependencies
Maintainers
1
Versions
86
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wikibase-sdk - npm Package Compare versions

Comparing version 9.2.4 to 9.2.5

dist/src/helpers/parse_snak.d.ts

36

dist/src/helpers/simplify_claims.d.ts

@@ -1,14 +0,28 @@

import type { Claim, Claims, PropertyClaims, PropertyQualifiers, Qualifier, Qualifiers, Reference } from '../types/claim.js';
import type { SimplifiedClaim, SimplifiedClaims, SimplifiedPropertyClaims, SimplifySnakOptions, SimplifySnaksOptions } from '../types/simplify_claims.js';
export declare function simplifySnaks(snaks: any, options: any): any;
export declare function simplifyPropertySnaks(propertySnaks: any, options: any): any;
export declare function simplifySnak(claim: any, options: any): any;
export declare function simplifyClaims(claims: Claims, options?: SimplifySnaksOptions): SimplifiedClaims;
export declare function simplifyPropertyClaims(propertyClaims: PropertyClaims, options?: SimplifySnaksOptions): SimplifiedPropertyClaims;
import type { Claim, Claims, PropertyClaims, PropertyQualifiers, PropertySnaks, Qualifier, Qualifiers, Reference, Snak, Snaks } from '../types/claim.js';
import type { SimplifiedClaim, SimplifiedClaims, SimplifiedPropertyClaims, SimplifiedPropertySnaks, SimplifiedSnaks, SimplifyClaimsOptions, SimplifySnakOptions, SimplifySnaksOptions } from '../types/simplify_claims.js';
/**
* Tries to replace wikidata deep snak object by a simple value
* e.g. a string, an entity Qid or an epoch time number
* Expects a single snak object
* Ex: entity.claims.P369[0]
*/
export declare function simplifySnak(snak: Snak, options?: SimplifySnakOptions): any;
export declare function simplifyClaim(claim: Claim, options?: SimplifySnakOptions): SimplifiedClaim;
export declare function simplifyQualifiers(qualifiers: Qualifiers, options?: SimplifySnaksOptions): any;
export declare function simplifyPropertyQualifiers(propertyQualifiers: PropertyQualifiers, options?: SimplifySnaksOptions): any;
export declare function simplifyClaims(claims: Claims, options?: SimplifyClaimsOptions): SimplifiedClaims;
export declare function simplifyPropertyClaims(propertyClaims: PropertyClaims, options?: SimplifyClaimsOptions): SimplifiedPropertyClaims;
export declare function simplifySnaks(snaks?: Snaks, options?: SimplifySnaksOptions): SimplifiedSnaks;
export declare function simplifyPropertySnaks(propertySnaks: PropertySnaks, options?: SimplifySnaksOptions): SimplifiedPropertySnaks;
export declare function simplifyQualifiers(qualifiers: Qualifiers, options?: SimplifySnaksOptions): SimplifiedSnaks;
export declare function simplifyPropertyQualifiers(propertyQualifiers: PropertyQualifiers, options?: SimplifySnaksOptions): SimplifiedPropertySnaks;
export declare function simplifyQualifier(qualifier: Qualifier, options?: SimplifySnakOptions): any;
export declare function simplifyReferences(references: readonly Reference[], options?: any): any[];
export declare function simplifyReferenceRecord(refRecord: Reference, options?: any): any;
export declare function simplifyReferences(references: readonly Reference[], options?: SimplifySnaksOptions): (SimplifiedSnaks | {
snaks: SimplifiedSnaks;
hash: string;
})[];
export declare function simplifyReference(reference: Reference, options?: SimplifySnaksOptions): SimplifiedSnaks | {
snaks: SimplifiedSnaks;
hash: string;
};
/** @deprecated use the new function name simplifyReference instead */
export declare const simplifyReferenceRecord: typeof simplifyReference;
//# sourceMappingURL=simplify_claims.d.ts.map

@@ -1,69 +0,16 @@

import { uniq } from '../utils/utils.js';
import { parseClaim } from './parse_claim.js';
import { isPlainObject, uniq } from '../utils/utils.js';
import { parseSnak } from './parse_snak.js';
import { truthyPropertyClaims, nonDeprecatedPropertyClaims } from './rank.js';
export function simplifySnaks(snaks, options) {
const { propertyPrefix } = options;
const simplifiedSnaks = {};
for (let id in snaks) {
const propertySnaks = snaks[id];
if (propertyPrefix) {
id = propertyPrefix + ':' + id;
}
simplifiedSnaks[id] = simplifyPropertySnaks(propertySnaks, options);
}
return simplifiedSnaks;
}
export function simplifyPropertySnaks(propertySnaks, options) {
// Avoid to throw on empty inputs to allow to simplify claims array
// without having to know if the entity as claims for this property
// Ex: simplifyPropertyClaims(entity.claims.P124211616)
if (propertySnaks == null || propertySnaks.length === 0)
return [];
const { keepNonTruthy, keepNonDeprecated, areSubSnaks } = parseKeepOptions(options);
if (keepNonDeprecated) {
propertySnaks = nonDeprecatedPropertyClaims(propertySnaks);
}
else if (!(keepNonTruthy || areSubSnaks)) {
propertySnaks = truthyPropertyClaims(propertySnaks);
}
propertySnaks = propertySnaks
.map(claim => simplifyClaim(claim, options))
// Filter-out novalue and somevalue claims,
// unless a novalueValue or a somevalueValue is passed in options
// Considers null as defined
.filter(obj => obj !== undefined);
// Deduplicate values unless we return a rich value object
if (propertySnaks[0] && typeof propertySnaks[0] !== 'object') {
return uniq(propertySnaks);
}
else {
return propertySnaks;
}
}
// Expects a single snak object
// Ex: entity.claims.P369[0]
export function simplifySnak(claim, options) {
const { keepQualifiers, keepReferences, keepIds, keepHashes, keepTypes, keepSnaktypes, keepRanks } = parseKeepOptions(options);
// tries to replace wikidata deep claim object by a simple value
// e.g. a string, an entity Qid or an epoch time number
const { mainsnak, rank } = claim;
let value, datatype, datavalue, snaktype, isQualifierSnak, isReferenceSnak;
if (mainsnak) {
datatype = mainsnak.datatype;
datavalue = mainsnak.datavalue;
snaktype = mainsnak.snaktype;
}
else {
// Qualifiers have no mainsnak, and define datatype, datavalue on claim
datavalue = claim.datavalue;
datatype = claim.datatype;
snaktype = claim.snaktype;
// Duck typing the sub-snak type
if (claim.hash)
isQualifierSnak = true;
else
isReferenceSnak = true;
}
/**
* Tries to replace wikidata deep snak object by a simple value
* e.g. a string, an entity Qid or an epoch time number
* Expects a single snak object
* Ex: entity.claims.P369[0]
*/
export function simplifySnak(snak, options = {}) {
const { keepTypes, keepSnaktypes, keepHashes } = parseKeepOptions(options);
let value;
const { datatype, datavalue, snaktype, hash } = snak;
if (datavalue) {
value = parseClaim(datatype, datavalue, options, claim.id);
value = parseSnak(datatype, datavalue, options);
}

@@ -78,9 +25,8 @@ else {

}
// Qualifiers should not attempt to keep sub-qualifiers or references
if (isQualifierSnak) {
if (!(keepHashes || keepTypes || keepSnaktypes))
return value;
// No need to test keepHashes as it has no effect if neither
// keepQualifiers or keepReferences is true
if (keepTypes || keepSnaktypes || keepHashes) {
// When keeping qualifiers or references, the value becomes an object
// instead of a direct value
const valueObj = { value };
if (keepHashes)
valueObj.hash = claim.hash;
if (keepTypes)

@@ -90,9 +36,14 @@ valueObj.type = datatype;

valueObj.snaktype = snaktype;
if (keepHashes)
valueObj.hash = hash;
return valueObj;
}
if (isReferenceSnak) {
if (!keepTypes)
return value;
return { type: datatype, value };
else {
return value;
}
}
export function simplifyClaim(claim, options = {}) {
const { keepQualifiers, keepReferences, keepIds, keepTypes, keepSnaktypes, keepRanks } = parseKeepOptions(options);
const { mainsnak, rank } = claim;
const value = simplifySnak(mainsnak, options);
// No need to test keepHashes as it has no effect if neither

@@ -103,19 +54,18 @@ // keepQualifiers or keepReferences is true

}
// When keeping qualifiers or references, the value becomes an object
// instead of a direct value
const valueObj = { value };
if (keepTypes)
valueObj.type = datatype;
if (keepSnaktypes)
valueObj.snaktype = snaktype;
// When keeping other attributes, the value becomes an object instead of a direct value
let valueObj;
if (isPlainObject(value)) {
valueObj = value;
}
else {
valueObj = { value };
}
if (keepRanks)
valueObj.rank = rank;
const subSnaksOptions = getSubSnakOptions(options);
subSnaksOptions.keepHashes = keepHashes;
if (keepQualifiers) {
valueObj.qualifiers = simplifyQualifiers(claim.qualifiers, subSnaksOptions);
valueObj.qualifiers = simplifyQualifiers(claim.qualifiers, options);
}
if (keepReferences) {
claim.references = claim.references || [];
valueObj.references = simplifyReferences(claim.references, subSnaksOptions);
valueObj.references = simplifyReferences(claim.references, options);
}

@@ -127,15 +77,58 @@ if (keepIds)

export function simplifyClaims(claims, options = {}) {
return simplifySnaks(claims, options);
return applyObjectSimplification(claims, simplifyPropertyClaims, options);
}
export function simplifyPropertyClaims(propertyClaims, options = {}) {
return simplifyPropertySnaks(propertyClaims, options);
// Avoid to throw on empty inputs to allow to simplify claims array
// without having to know if the entity as claims for this property
// Ex: simplifyPropertyClaims(entity.claims.P124211616)
if (propertyClaims == null || propertyClaims.length === 0)
return [];
const { keepNonTruthy, keepNonDeprecated } = parseKeepOptions(options);
if (keepNonDeprecated) {
propertyClaims = nonDeprecatedPropertyClaims(propertyClaims);
}
else if (!(keepNonTruthy)) {
propertyClaims = truthyPropertyClaims(propertyClaims);
}
return applyArraySimplification(propertyClaims, simplifyClaim, options);
}
export function simplifyClaim(claim, options = {}) {
return simplifySnak(claim, options);
export function simplifySnaks(snaks = {}, options = {}) {
return applyObjectSimplification(snaks, simplifyPropertySnaks, options);
}
export function simplifyPropertySnaks(propertySnaks, options = {}) {
if (propertySnaks == null || propertySnaks.length === 0)
return [];
return applyArraySimplification(propertySnaks, simplifySnak, options);
}
function applyObjectSimplification(obj, simplifyFn, options) {
const { propertyPrefix } = options;
const simplified = {};
for (let [propertyId, propertyArray] of Object.entries(obj)) {
if (propertyPrefix) {
propertyId = propertyPrefix + ':' + propertyId;
}
simplified[propertyId] = simplifyFn(propertyArray, options);
}
return simplified;
}
function applyArraySimplification(array, simplifyFn, options) {
const simplifiedArray = array
.map(claimOrSnak => simplifyFn(claimOrSnak, options))
// Filter-out novalue and somevalue claims,
// unless a novalueValue or a somevalueValue is passed in options
// Considers null as defined
.filter(obj => obj !== undefined);
// Deduplicate values unless we return a rich value object
if (simplifiedArray[0] && typeof simplifiedArray[0] !== 'object') {
return uniq(simplifiedArray);
}
else {
return simplifiedArray;
}
}
export function simplifyQualifiers(qualifiers, options = {}) {
return simplifySnaks(qualifiers, getSubSnakOptions(options));
return simplifySnaks(qualifiers, options);
}
export function simplifyPropertyQualifiers(propertyQualifiers, options = {}) {
return simplifyPropertySnaks(propertyQualifiers, getSubSnakOptions(options));
return simplifyPropertySnaks(propertyQualifiers, options);
}

@@ -146,26 +139,20 @@ export function simplifyQualifier(qualifier, options = {}) {

export function simplifyReferences(references, options = {}) {
return references.map(refRecord => simplifyReferenceRecord(refRecord, options));
return references.map(reference => simplifyReference(reference, options));
}
export function simplifyReferenceRecord(refRecord, options = {}) {
const subSnaksOptions = getSubSnakOptions(options);
const snaks = simplifySnaks(refRecord.snaks, subSnaksOptions);
if (subSnaksOptions.keepHashes)
return { snaks, hash: refRecord.hash };
export function simplifyReference(reference, options = {}) {
const snaks = simplifySnaks(reference.snaks, options);
if (options.keepHashes)
return { snaks, hash: reference.hash };
else
return snaks;
}
const getSubSnakOptions = (options = {}) => {
if (options.areSubSnaks)
return options;
// Using a new object so that the original options object isn't modified
else
return Object.assign({}, options, { areSubSnaks: true });
};
/** @deprecated use the new function name simplifyReference instead */
export const simplifyReferenceRecord = simplifyReference;
const keepOptions = ['keepQualifiers', 'keepReferences', 'keepIds', 'keepHashes', 'keepTypes', 'keepSnaktypes', 'keepRanks', 'keepRichValues'];
const parseKeepOptions = (options = {}) => {
if (options.keepAll) {
keepOptions.forEach(optionName => {
for (const optionName of keepOptions) {
if (options[optionName] == null)
options[optionName] = true;
});
}
}

@@ -172,0 +159,0 @@ return options;

@@ -19,2 +19,4 @@ import { simplifyClaims } from './simplify_claims.js';

const { type } = entity;
if (!type)
throw new Error('missing entity type');
const simplified = {

@@ -60,3 +62,3 @@ id: entity.id,

export const simplifyEntities = (entities, options = {}) => {
// @ts-expect-error
// @ts-expect-error support downloaded json directly
if (entities.entities)

@@ -63,0 +65,0 @@ entities = entities.entities;

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

export { simplifyClaim as claim, simplifyClaims as claims, simplifyPropertyClaims as propertyClaims, simplifyPropertyQualifiers as propertyQualifiers, simplifyQualifier as qualifier, simplifyQualifiers as qualifiers, simplifyReferences as references, } from './simplify_claims.js';
export { simplifyClaim as claim, simplifyClaims as claims, simplifyPropertyClaims as propertyClaims, simplifyPropertyQualifiers as propertyQualifiers, simplifyQualifier as qualifier, simplifyQualifiers as qualifiers, simplifyReference as reference, simplifyReferences as references, simplifySnak as snak, } from './simplify_claims.js';
export { simplifyForm as form, simplifyForms as forms, } from './simplify_forms.js';

@@ -8,3 +8,2 @@ export { simplifySense as sense, simplifySenses as senses, } from './simplify_senses.js';

export { simplifyEntities as entities, simplifyEntity as entity, } from './simplify_entity.js';
export { simplifyClaim as snak, simplifyClaims as snaks, simplifyPropertyClaims as propertySnaks, } from './simplify_claims.js';
//# sourceMappingURL=simplify.d.ts.map

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

export { simplifyClaim as claim, simplifyClaims as claims, simplifyPropertyClaims as propertyClaims, simplifyPropertyQualifiers as propertyQualifiers, simplifyQualifier as qualifier, simplifyQualifiers as qualifiers, simplifyReferences as references, } from './simplify_claims.js';
export { simplifyClaim as claim, simplifyClaims as claims, simplifyPropertyClaims as propertyClaims, simplifyPropertyQualifiers as propertyQualifiers, simplifyQualifier as qualifier, simplifyQualifiers as qualifiers, simplifyReference as reference, simplifyReferences as references, simplifySnak as snak, } from './simplify_claims.js';
export { simplifyForm as form, simplifyForms as forms, } from './simplify_forms.js';

@@ -8,4 +8,2 @@ export { simplifySense as sense, simplifySenses as senses, } from './simplify_senses.js';

export { simplifyEntities as entities, simplifyEntity as entity, } from './simplify_entity.js';
// Aliases
export { simplifyClaim as snak, simplifyClaims as snaks, simplifyPropertyClaims as propertySnaks, } from './simplify_claims.js';
//# sourceMappingURL=simplify.js.map

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

import type { PropertyId } from './entity.js';
import type { Guid, PropertyId } from './entity.js';
import type { SnakValue } from './snakvalue.js';
import type { parsers } from '../helpers/parse_claim.js';
import type { parsers } from '../helpers/parse_snak.js';
export type Rank = 'normal' | 'preferred' | 'deprecated';

@@ -8,3 +8,3 @@ export type SnakType = 'value' | 'somevalue' | 'novalue';

export interface Claim {
id: string;
id: Guid;
mainsnak: Snak;

@@ -11,0 +11,0 @@ rank: Rank;

@@ -27,7 +27,6 @@ import type { Claims, DataType } from './claim.js';

export type Hash = string;
export type Entity = (Property | Item | Lexeme | MediaInfo);
export type Entity = Property | Item | Lexeme | MediaInfo;
export type EntityPageTitle = NamespacedEntityId | ItemId;
export type Entities = Record<EntityId, Entity>;
export interface Property extends EntityInfo {
id: PropertyId;
export interface Property extends EntityInfo<PropertyId> {
type: 'property';

@@ -40,4 +39,3 @@ datatype?: DataType;

}
export interface Item extends EntityInfo {
id: ItemId;
export interface Item extends EntityInfo<ItemId> {
type: 'item';

@@ -50,4 +48,3 @@ labels?: Labels;

}
export interface Lexeme extends EntityInfo {
id: LexemeId;
export interface Lexeme extends EntityInfo<LexemeId> {
type: 'lexeme';

@@ -61,4 +58,3 @@ lexicalCategory: ItemId;

}
export interface MediaInfo extends EntityInfo {
id: MediaInfoId;
export interface MediaInfo extends EntityInfo<MediaInfoId> {
type: 'mediainfo';

@@ -69,3 +65,3 @@ labels?: Labels;

}
export interface EntityInfo {
export interface EntityInfo<T> {
pageid?: number;

@@ -77,5 +73,6 @@ ns?: number;

redirects?: {
from: string;
to: string;
from: T;
to: T;
};
id: T;
}

@@ -82,0 +79,0 @@ export interface SimplifiedEntityInfo {

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

import type { SimplifySnaksOptions } from './simplify_claims.js';
import type { SimplifyClaimsOptions } from './simplify_claims.js';
export interface InstanceConfig {

@@ -10,3 +10,3 @@ instance?: string;

export type LanguageCode = string;
export interface SimplifyEntityOptions extends SimplifySnaksOptions, SimplifySitelinkOptions {
export interface SimplifyEntityOptions extends SimplifyClaimsOptions, SimplifySitelinkOptions {
}

@@ -13,0 +13,0 @@ export interface SimplifySitelinkOptions {

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

import type { DataType, Rank } from './claim.js';
import type { Guid, PropertyId } from './entity.js';
import type { timeConverters } from '../helpers/parse_claim.js';
import type { DataType, Rank, SnakType } from './claim.js';
import type { Guid, Hash, PropertyId } from './entity.js';
import type { timeConverters } from '../helpers/parse_snak.js';
export interface SimplifySnakOptions {

@@ -11,5 +11,3 @@ entityPrefix?: string;

keepReferences?: boolean;
keepIds?: boolean;
keepHashes?: boolean;
keepRanks?: boolean;
keepSnaktypes?: boolean;

@@ -21,9 +19,11 @@ keepAll?: boolean;

}
export interface SimplifySnaksOptions extends SimplifySnakOptions {
export type SimplifySnaksOptions = SimplifySnakOptions;
export interface SimplifyClaimsOptions extends SimplifySnaksOptions {
keepIds?: boolean;
keepRanks?: boolean;
keepNonTruthy?: boolean;
keepNonDeprecated?: boolean;
}
export type SimplifyClaimsOptions = SimplifySnaksOptions;
export interface CustomSimplifiedClaim extends CustomSimplifiedSnak {
id: Guid;
id?: Guid;
rank?: Rank;

@@ -43,5 +43,8 @@ qualifiers?: SimplifiedQualifiers;

export type SimplifiedReferenceSnak = SimplifiedSnak;
export interface SimplifiedReference {
[property: string]: SimplifiedReferenceSnak;
export type SimplifiedReferenceSnaks = Record<PropertyId, SimplifiedReferenceSnak[]>;
export interface RichSimplifiedReferenceSnaks {
snaks: SimplifiedReferenceSnaks;
hash: Hash;
}
export type SimplifiedReference = SimplifiedReferenceSnaks | RichSimplifiedReferenceSnaks;
export type SimplifiedReferences = SimplifiedReference[];

@@ -51,6 +54,7 @@ export type SimplifiedSnak = string | number | CustomSimplifiedSnak;

export interface CustomSimplifiedSnak {
id: string;
type?: DataType;
value: unknown;
snaktype?: SnakType;
hash?: Hash;
}
//# sourceMappingURL=simplify_claims.d.ts.map
import type { ItemId } from './entity.js';
import type { Site } from '../helpers/wikimedia_constants.js';
import type { Url } from '../utils/build_url.js';
export type SitelinkBadges = ItemId[];
export interface Sitelink {
site: Site;
title: string;
badges: ItemId[];
badges: SitelinkBadges;
url?: Url;

@@ -9,0 +10,0 @@ }

@@ -8,3 +8,3 @@ /** Example: keep only 'fr' in 'fr_FR' */

export declare function forceArray<T extends string>(array: T | readonly T[] | undefined): T[];
/** simplistic implementation to filter-out arrays */
/** simplistic implementation to filter-out arrays and null */
export declare function isPlainObject(obj: unknown): boolean;

@@ -11,0 +11,0 @@ export declare const fixedEncodeURIComponent: (str: string) => string;

@@ -20,3 +20,3 @@ /** Example: keep only 'fr' in 'fr_FR' */

}
/** simplistic implementation to filter-out arrays */
/** simplistic implementation to filter-out arrays and null */
export function isPlainObject(obj) {

@@ -23,0 +23,0 @@ return Boolean(obj) && typeof obj === 'object' && !Array.isArray(obj);

{
"name": "wikibase-sdk",
"version": "9.2.4",
"version": "9.2.5",
"type": "module",

@@ -5,0 +5,0 @@ "description": "utils functions to query a Wikibase instance and simplify its results",

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