Skip to content

Commit

Permalink
update citation-js core
Browse files Browse the repository at this point in the history
  • Loading branch information
timlrx committed Oct 12, 2023
1 parent 4a654f3 commit 9427ed1
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 80 deletions.
8 changes: 3 additions & 5 deletions src/citation-js/core/Cite/async.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//@ts-nocheck
import Cite from './index.js'

// @ts-nocheck
/**
* @callback module:@citation-js/core.Cite~asyncCallback
* @param {Cite} data - Cite object
Expand All @@ -22,7 +20,7 @@ function async(data, options, callback) {
options = undefined
}

const promise = Cite().setAsync(data, options)
const promise = (new this()).setAsync(data, options)

if (typeof callback === 'function') {
promise.then(callback)
Expand All @@ -32,4 +30,4 @@ function async(data, options, callback) {
}
}

export default async
export default async
16 changes: 5 additions & 11 deletions src/citation-js/core/Cite/get.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
import { validateOutputOptions as validate } from './static.js'
import { validateOutputOptions as validate } from './validate.js'
import { format as formatData } from '../plugins/output.js'
import { clean as parseCsl } from '../plugins/input/csl.js'

Expand All @@ -13,7 +13,7 @@ import { clean as parseCsl } from '../plugins/input/csl.js'
* @return {Array<String>} List of IDs
*/
export function getIds() {
return this.data.map((entry) => entry.id)
return this.data.map(entry => entry.id)
}

/**
Expand Down Expand Up @@ -53,8 +53,7 @@ export function get(options = {}) {

const { type, style } = parsedOptions
const [styleType, styleFormat] = style.split('-')
const newStyle =
styleType === 'citation' ? 'bibliography' : styleType === 'csl' ? 'data' : styleType
const newStyle = styleType === 'citation' ? 'bibliography' : styleType === 'csl' ? 'data' : styleType
const newType = type === 'string' ? 'text' : type === 'json' ? 'object' : type

let formatOptions
Expand All @@ -81,12 +80,7 @@ export function get(options = {}) {
const result = this.format(newStyle, Object.assign(formatOptions, options._newOptions))

const { format } = parsedOptions
if (
format === 'real' &&
newType === 'html' &&
typeof document !== 'undefined' &&
typeof document.createElement === 'function'
) {
if (format === 'real' && newType === 'html' && typeof document !== 'undefined' && typeof document.createElement === 'function') {
const tmp = document.createElement('div')
tmp.innerHTML = result
return tmp.firstChild
Expand All @@ -95,4 +89,4 @@ export function get(options = {}) {
} else {
return result
}
}
}
11 changes: 5 additions & 6 deletions src/citation-js/core/Cite/log.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Cite from './index.js'

// @ts-nocheck
/**
* @memberof module:@citation-js/core.Cite#
*
* @memberof module: @citation-js / core.Cite#
*
* @return {Number} The latest version of the object
*/
function currentVersion() {
Expand All @@ -23,7 +22,7 @@ function retrieveVersion(versnum = 1) {
return null
} else {
const [data, options] = this.log[versnum - 1]
const image = new Cite(JSON.parse(data), JSON.parse(options))
const image = new this.constructor(JSON.parse(data), JSON.parse(options))
image.log = this.log.slice(0, versnum)
return image
}
Expand Down Expand Up @@ -66,4 +65,4 @@ function save() {
return this
}

export { currentVersion, retrieveVersion, retrieveLastVersion, undo, save }
export { currentVersion, retrieveVersion, retrieveLastVersion, undo, save }
39 changes: 19 additions & 20 deletions src/citation-js/core/plugin-common/input/index.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
// @ts-nocheck
/* global jQuery, HTMLElement */

/**
* @module input/other
*/

import * as empty from './empty.js'
import * as url from './url.js'
import * as json from './json.js'
import * as jquery from './jquery.js'
import * as html from './html.js'

export const ref = '@else'
export const parsers = { empty, url, json, jquery, html }
export const parsers = { empty, json, jquery, html }
export const formats = {
'@empty/text': {
parse: empty.parse,
parseType: {
dataType: 'String',
predicate: (input) => input === '',
},
predicate: input => input === ''
}
},
'@empty/whitespace+text': {
parse: empty.parse,
parseType: {
dataType: 'String',
predicate: /^\s+$/,
},
predicate: /^\s+$/
}
},
'@empty': {
parse: empty.parse,
parseType: {
dataType: 'Primitive',
predicate: (input) => input == null,
},
predicate: input => input == null
}
},
'@else/json': {
parse: json.parse,
parseType: {
dataType: 'String',
predicate: /^\s*(\{[\S\s]*\}|\[[\S\s]*\])\s*$/,
},
predicate: /^\s*(\{[\S\s]*\}|\[[\S\s]*\])\s*$/
}
},
'@else/url': {
parse: url.parse,
parseAsync: url.parseAsync,
parseType: {
dataType: 'String',
predicate: /^https?:\/\/(([\w-]+\.)*[\w-]+)(:\d+)?(\/[^?/]*)*(\?[^#]*)?(#.*)?$/i,
},
predicate: /^https?:\/\/(([\w-]+\.)*[\w-]+)(:\d+)?(\/[^?/]*)*(\?[^#]*)?(#.*)?$/i
}
},
'@else/jquery': {
parse: jquery.parse,
Expand All @@ -55,8 +54,8 @@ export const formats = {
/* istanbul ignore next: not testable in Node */
predicate(input) {
return typeof jQuery !== 'undefined' && input instanceof jQuery
},
},
}
}
},
'@else/html': {
parse: html.parse,
Expand All @@ -65,7 +64,7 @@ export const formats = {
/* istanbul ignore next: not testable in Node */
predicate(input) {
return typeof HTMLElement !== 'undefined' && input instanceof HTMLElement
},
},
},
}
}
}
}
}
1 change: 0 additions & 1 deletion src/citation-js/core/plugin-common/input/url.js

This file was deleted.

68 changes: 32 additions & 36 deletions src/citation-js/core/plugins/input/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,23 @@ import { applyGraph, removeGraph } from './graph.js'
/**
* @access private
* @param {Array<Object>} graph
* @return {String}
*/
function prepareParseGraph(graph) {
return (
graph
// collapse continuous iterations of the same type
.reduce((array, next) => {
const last = array[array.length - 1]
if (last && last.type === next.type) {
last.count = last.count + 1 || 2
} else {
array.push(next)
}
return array
}, [])
// presentation
.map((element) => (element.count > 1 ? element.count + 'x ' : '') + element.type)
.join(' -> ')
)
return graph
// collapse continuous iterations of the same type
.reduce((array, next) => {
const last = array[array.length - 1]
if (last && last.type === next.type) {
last.count = last.count + 1 || 2
} else {
array.push(next)
}
return array
}, [])
// presentation
.map(element => (element.count > 1 ? element.count + 'x ' : '') + element.type)
.join(' -> ')
}

/**
Expand All @@ -40,20 +39,19 @@ function prepareParseGraph(graph) {
*/
class ChainParser {
constructor(input, options = {}) {
this.options = Object.assign(
{
generateGraph: true,
forceType: parseType(input),
maxChainLength: 10,
strict: true,
target: '@csl/list+object',
},
options
)
this.options = Object.assign({
generateGraph: true,
forceType: parseType(input),
maxChainLength: 10,
strict: true,
target: '@csl/list+object'
}, options)

this.type = this.options.forceType
this.data = typeof input === 'object' ? deepCopy(input) : input
this.graph = [{ type: this.type, data: input }]
this.graph = [
{ type: this.type, data: input }
]
this.iteration = 0
}

Expand All @@ -79,9 +77,8 @@ class ChainParser {
if (this.error || this.type === this.options.target) {
return false
} else if (this.iteration >= this.options.maxChainLength) {
this.error = new RangeError(
`Max. number of parsing iterations reached (${prepareParseGraph(this.graph)})`
)
this.error = new RangeError(`Max. number of parsing iterations reached (${prepareParseGraph(this.graph)
})`)
return false
} else {
this.iteration++
Expand All @@ -104,8 +101,9 @@ class ChainParser {
return []
}
} else if (this.options.target === '@csl/list+object') {
return upgradeCsl(this.data).map(
this.options.generateGraph ? (entry) => applyGraph(entry, this.graph) : removeGraph
return upgradeCsl(this.data).map(this.options.generateGraph
? entry => applyGraph(entry, this.graph)
: removeGraph
)
} else {
return this.data
Expand Down Expand Up @@ -173,9 +171,7 @@ export const chainAsync = async (...args) => {
const chain = new ChainParser(...args)

while (chain.iterate()) {
chain.data = await parseDataAsync(chain.data, chain.type).catch((e) => {
chain.error = e
})
chain.data = await parseDataAsync(chain.data, chain.type).catch(e => { chain.error = e })
}

return chain.end()
Expand All @@ -197,4 +193,4 @@ export const chainLinkAsync = async (input) => {
const output = type.match(/array|object/) ? deepCopy(input) : input

return parseDataAsync(output, type)
}
}
1 change: 0 additions & 1 deletion src/cite.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ function Cite(data, opts) {
}

const self = new CiteCore(data, opts)
console.log(self)
this._options = self._options
this.log = self.log
this.data = self.data
Expand Down

0 comments on commit 9427ed1

Please sign in to comment.