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 13, 2023
1 parent 9427ed1 commit caf8979
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/citation-js/core/Cite/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function async(data, options, callback) {
options = undefined
}

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

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

export default async
export default async
14 changes: 10 additions & 4 deletions src/citation-js/core/Cite/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,7 +53,8 @@ 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 @@ -80,7 +81,12 @@ 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 @@ -89,4 +95,4 @@ export function get(options = {}) {
} else {
return result
}
}
}
6 changes: 3 additions & 3 deletions src/citation-js/core/Cite/log.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @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 Down Expand Up @@ -65,4 +65,4 @@ function save() {
return this
}

export { currentVersion, retrieveVersion, retrieveLastVersion, undo, save }
export { currentVersion, retrieveVersion, retrieveLastVersion, undo, save }
32 changes: 16 additions & 16 deletions src/citation-js/core/plugin-common/input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,35 @@ export const formats = {
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': {
parseType: {
dataType: 'String',
predicate: /^https?:\/\/(([\w-]+\.)*[\w-]+)(:\d+)?(\/[^?/]*)*(\?[^#]*)?(#.*)?$/i
}
predicate: /^https?:\/\/(([\w-]+\.)*[\w-]+)(:\d+)?(\/[^?/]*)*(\?[^#]*)?(#.*)?$/i,
},
},
'@else/jquery': {
parse: jquery.parse,
Expand All @@ -54,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 @@ -64,7 +64,7 @@ export const formats = {
/* istanbul ignore next: not testable in Node */
predicate(input) {
return typeof HTMLElement !== 'undefined' && input instanceof HTMLElement
}
}
}
}
},
},
},
}
67 changes: 36 additions & 31 deletions src/citation-js/core/plugins/input/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ import { applyGraph, removeGraph } from './graph.js'
* @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 @@ -39,19 +41,20 @@ 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 @@ -77,8 +80,9 @@ 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 @@ -101,9 +105,8 @@ 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 @@ -171,7 +174,9 @@ 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 @@ -193,4 +198,4 @@ export const chainLinkAsync = async (input) => {
const output = type.match(/array|object/) ? deepCopy(input) : input

return parseDataAsync(output, type)
}
}

0 comments on commit caf8979

Please sign in to comment.