-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3830fff
commit c0f68c9
Showing
4 changed files
with
65 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { NamedNode } from '@rdfjs/types' | ||
import { xsd } from '@tpluscode/rdf-ns-builders' | ||
import { validators } from 'rdf-validate-datatype' | ||
|
||
type Validator = (value: string) => boolean | ||
|
||
interface Datatype { | ||
check: Validator | ||
name: NamedNode | ||
} | ||
|
||
export class DatatypeChecker { | ||
private datatypes: Datatype[] = [] | ||
|
||
constructor() { | ||
this.add(xsd.integer) | ||
this.add(xsd.decimal) | ||
this.add(xsd.date) | ||
} | ||
|
||
private add(name: NamedNode) { | ||
const check = validators.find(name) | ||
if (check) { | ||
this.datatypes.push({ check, name }) | ||
} | ||
} | ||
|
||
public determineDatatype(values: string[]): NamedNode { | ||
let i = 0 | ||
let current = this.datatypes[i] | ||
for (const value of values) { | ||
while (!current.check(value)) { | ||
if (++i === this.datatypes.length) { | ||
return xsd.string | ||
} | ||
current = this.datatypes[i] | ||
} | ||
} | ||
return current.name | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,16 @@ | ||
import { describe, it } from 'mocha' | ||
import { expect } from 'chai' | ||
import { validators } from 'rdf-validate-datatype' | ||
import { xsd } from '@tpluscode/rdf-ns-builders' | ||
import { NamedNode } from '@rdfjs/types' | ||
import { DatatypeChecker } from '../DatatypeChecker' | ||
|
||
type Validator = (value: string) => boolean | ||
const datatypeChecker = new DatatypeChecker() | ||
|
||
const datatypes: Array<{ check: Validator; name: NamedNode }> = [] | ||
|
||
const add = (name: NamedNode) => { | ||
const check = validators.find(name) | ||
if (check) { | ||
datatypes.push({ check, name }) | ||
} | ||
} | ||
|
||
add(xsd.integer) | ||
add(xsd.decimal) | ||
add(xsd.date) | ||
|
||
const datatype = (values: string[]) => { | ||
let i = 0 | ||
let current = datatypes[i] | ||
for (const value of values) { | ||
while (!current.check(value)) { | ||
if (++i === datatypes.length) { | ||
return xsd.string | ||
} | ||
current = datatypes[i] | ||
} | ||
} | ||
return current.name | ||
} | ||
|
||
describe.only('@cube-creator/model/CsvColumn', () => { | ||
describe('columnDatatype', () => { | ||
it('recognize integers', () => { | ||
const isInteger = validators.find(xsd.integer) | ||
expect(isInteger).to.be.not.null | ||
if (isInteger) { | ||
expect(isInteger('42')).to.be.true | ||
expect(isInteger('-42')).to.be.true | ||
expect(isInteger('foo')).to.be.false | ||
expect(isInteger('42.0')).to.be.false | ||
expect(isInteger('2021-01-01')).to.be.false | ||
} | ||
}) | ||
it('recognize decimals', () => { | ||
const isDecimal = validators.find(xsd.decimal) | ||
expect(isDecimal).to.be.not.null | ||
if (isDecimal) { | ||
expect(isDecimal('42')).to.be.true | ||
expect(isDecimal('-42')).to.be.true | ||
expect(isDecimal('foo')).to.be.false | ||
expect(isDecimal('42.0')).to.be.true | ||
expect(isDecimal('2021-01-01')).to.be.false | ||
} | ||
}) | ||
it('recognize dates', () => { | ||
const isDate = validators.find(xsd.date) | ||
expect(isDate).to.be.not.null | ||
if (isDate) { | ||
expect(isDate('2021-01-01')).to.be.true | ||
expect(isDate('2021-01-01T00:00:00Z')).to.be.false | ||
expect(isDate('foo')).to.be.false | ||
} | ||
}) | ||
}) | ||
describe('datatype', () => { | ||
it('recognize datatype', () => { | ||
expect(datatype(['42']).equals(xsd.integer)).to.be.true | ||
expect(datatype(['42', '42']).equals(xsd.integer)).to.be.true | ||
expect(datatype(['42', '42.1']).equals(xsd.decimal)).to.be.true | ||
expect(datatype(['42', 'foo']).equals(xsd.string)).to.be.true | ||
expect(datatype(['2021-01-01', '2021-01-01']).equals(xsd.date)).to.be.true | ||
}) | ||
describe('@cube-creator/model/DatatypeChecker', () => { | ||
it('recognize datatype', () => { | ||
expect(datatypeChecker.determineDatatype(['42']).equals(xsd.integer)).to.be.true | ||
expect(datatypeChecker.determineDatatype(['42', '42']).equals(xsd.integer)).to.be.true | ||
expect(datatypeChecker.determineDatatype(['42', '42.1']).equals(xsd.decimal)).to.be.true | ||
expect(datatypeChecker.determineDatatype(['42', 'foo']).equals(xsd.string)).to.be.true | ||
expect(datatypeChecker.determineDatatype(['2021-01-01', '2021-01-01']).equals(xsd.date)).to.be.true | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters