Skip to content

Commit

Permalink
Implement type transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed Dec 29, 2024
1 parent 9ba554d commit fd552ca
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ the transformers set as properties:
- [`template`](#template)
- [`trim`](#trim)
- [`truncate`](#truncate)
- [`type`](#type)
- [`unique`](#unique)
- [`uppercase`](#uppercase)
- [`uriPart`](#uripart)
Expand Down Expand Up @@ -599,6 +600,26 @@ When a `length` property is set, a given string that is longer than this length
is shortened. If a `postfix` is given, it is appended to the end and the total
length of the shortened text will still be no longer than `length`.

### `type`

The transformer will return a string indicating what type the pipeline data is.
The known types are:

- `string`
- `number`
- `boolean`
- `date`
- `null`
- `undefined`
- `object`
- `array`

This transformer works the same in both directions, as there is no way to go
back to the original value. (If you do run data produced by this tranformer back
in reverse, you will always end up with `'string'`, which is not very
meaningful, but again -- there is no way to reproduce the value that we started
with.)

### `unique`

Generates a universally unique id. The incoming value is disregarded.
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import sum from './sum.js'
import { template, parse } from './template.js'
import trim from './trim.js'
import truncate from './truncate.js'
import type from './type.js'
import unique from './unique.js'
import uppercase from './uppercase.js'
import uriPart from './uriPart.js'
Expand Down Expand Up @@ -74,6 +75,7 @@ const transformers: Record<string, Transformer | AsyncTransformer> = {
template,
trim,
truncate,
type,
unique,
uppercase,
uriPart,
Expand Down
45 changes: 45 additions & 0 deletions src/type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import test from 'node:test'
import assert from 'node:assert/strict'

import transformer from './type.js'

// Setup

const options = {}

const state = {
rev: false,
onlyMappedValues: false,
context: [],
value: {},
}

const stateRev = { ...state, rev: true }

// Tests -- forward

test('should return the type a value', () => {
assert.equal(transformer({})(options)('I am a string', state), 'string')
assert.equal(transformer({})(options)(32, state), 'number')
assert.equal(transformer({})(options)(32.4, state), 'number')
assert.equal(transformer({})(options)(true, state), 'boolean')
assert.equal(transformer({})(options)(false, state), 'boolean')
assert.equal(transformer({})(options)(new Date(), state), 'date')
assert.equal(transformer({})(options)({}, state), 'object')
assert.equal(transformer({})(options)(null, state), 'null')
assert.equal(transformer({})(options)(undefined, state), 'undefined')
})

// Tests -- reverse

test('should return the type a value in reverse', () => {
assert.equal(transformer({})(options)('I am a string', stateRev), 'string')
assert.equal(transformer({})(options)(32, stateRev), 'number')
assert.equal(transformer({})(options)(32.4, stateRev), 'number')
assert.equal(transformer({})(options)(true, stateRev), 'boolean')
assert.equal(transformer({})(options)(false, stateRev), 'boolean')
assert.equal(transformer({})(options)(new Date(), stateRev), 'date')
assert.equal(transformer({})(options)({}, stateRev), 'object')
assert.equal(transformer({})(options)(null, stateRev), 'null')
assert.equal(transformer({})(options)(undefined, stateRev), 'undefined')
})
15 changes: 15 additions & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { isDate } from './utils/is.js'
import type { Transformer } from 'integreat'

const transformer: Transformer = () => () =>
function type(data) {
if (isDate(data)) {
return 'date'
} else if (data === null) {
return 'null'
} else {
return typeof data
}
}

export default transformer

0 comments on commit fd552ca

Please sign in to comment.