-
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.
use rdf validators to guess datatypes
- Loading branch information
1 parent
ed3202e
commit 3830fff
Showing
3 changed files
with
74 additions
and
38 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
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,59 +1,79 @@ | ||
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' | ||
|
||
const isInteger = (value: string) => /^-?\d+$/.test(value) | ||
const isDecimal = (value: string) => /^-?\d+(\.\d+)?$/.test(value) | ||
const isDate = (value: string) => /^\d{4}-\d{2}-\d{2}$/.test(value) | ||
type Validator = (value: string) => boolean | ||
|
||
const datatypes = [ | ||
{ check: isInteger, name: 'integer' }, | ||
{ check: isDecimal, name: 'decimal' }, | ||
{ check: isDate, name: 'date' }, | ||
] | ||
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 'string' | ||
return xsd.string | ||
} | ||
current = datatypes[i] | ||
} | ||
} | ||
return current.name | ||
} | ||
|
||
describe('@cube-creator/model/CsvColumn', () => { | ||
describe.only('@cube-creator/model/CsvColumn', () => { | ||
describe('columnDatatype', () => { | ||
it('recognize integers', () => { | ||
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 | ||
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', () => { | ||
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 | ||
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', () => { | ||
expect(isDate('2021-01-01')).to.be.true | ||
expect(isDate('2021-01-01T00:00:00Z')).to.be.false | ||
expect(isDate('foo')).to.be.false | ||
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'])).to.equal('integer') | ||
expect(datatype(['42', '42'])).to.equal('integer') | ||
expect(datatype(['42', '42.1'])).to.equal('decimal') | ||
expect(datatype(['42', 'foo'])).to.equal('string') | ||
expect(datatype(['2021-01-01', '2021-01-01'])).to.equal('date') | ||
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 | ||
}) | ||
}) | ||
}) |
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