Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,48 @@

Transforms fixed string to object and vice versa

## Javascript
Is available in both functional and object oriented styles, for backwards compatibility. Although we recommend using the functional style.

```javascript
var transformer = new fixedstr([
fixedstr.str('foo', 2),
fixedstr.str('bar', 5),
fixedstr.number('baz', 3)
])
## Functional style

Given the object definitions, you can either use currying to objectify/stringify, or use a factory function that curries for you.

### Define objects

Object definitions can be created using a helper function, that takes in a property name and length of property value

```typescript
const objectDefinitions: IObjectDefinition[] = [
stringObject('foo', 2),
stringObject('bar', 5),
numberObject('baz', 3)
]
```

### Currying method

```typescript
curriedObjectify(objectDefinitions)('F Bar 012') // {foo: 'F', bar: 'Bar', baz: 12}
curriedstringify(objectDefinitions)({ foo: 'F', bar: 'Bar', baz: 12 }) // 'F Bar 012'
```

### Factory method

```typescript
const transformer = createFixedStr(objectDefinitions)
transformer.objectify('F Bar 012') // {foo: 'F', bar: 'Bar', baz: 12}
transformer.stringify({ foo: 'F', bar: 'Bar', baz: 12 }) // 'F Bar 012'
```

## Typescript
## Object oriented style

```typescript
import { FixedStr } from 'fixedstr'
interface ITestObject {
foo: string
bar: string
bas: number
}
const transformer = new FixedStr([
FixedStr.str('foo', 2),
FixedStr.str('bar', 5),
FixedStr.number('baz', 3)
])
transformer.objectify<ITestObject>('F Bar 012') // {foo: 'F', bar: 'Bar', baz: 12}:ITestObject
transformer.objectify('F Bar 012') // {foo: 'F', bar: 'Bar', baz: 12}
transformer.stringify({ foo: 'F', bar: 'Bar', baz: 12 }) // 'F Bar 012'
```
61 changes: 60 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "fixedstr",
"author": "Tryggingamiðstöðin <bt.hugbunadargerd@tm.is>",
"version": "1.0.3",
"description": "Transforms fixed string to object and vice versa",
"version": "1.1.0",
"description": "Transforms fixed string to object and vice versa",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
Expand All @@ -26,12 +26,13 @@
"object",
"object to string",
"objectify"
],
],
"scripts": {
"lint": "tslint ./src/**",
"format-verify": "prettier -l ./src/**",
"format": "prettier --write ./src/**",
"test": "mocha dist/test",
"tdd": "mocha --exit -R min --require ts-node/register --recursive --watch-extensions ts,js --watch 'src/test.ts'",
"compile": "tsc",
"clean": "rm -rf ./dist && tsc",
"ci": "npm run clean && npm run lint && npm run format-verify && npm run test"
Expand All @@ -46,7 +47,8 @@
"prettier": "^1.14.3",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.15.0",
"typescript": "^3.1.3"
"typescript": "^3.1.3",
"ts-node": "^8.5.2"
},
"husky": {
"hooks": {
Expand Down
99 changes: 72 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,87 @@ export interface IFixedStr {
stringify: (obj: object) => string
}

export class FixedStr implements IFixedStr {
public static str(name: string, size: number): IObjectDefinition {
return {
name: name,
size: size
export function stringObject(name: string, size: number): IObjectDefinition {
return {
name: name,
size: size
}
}
export function numberObject(name: string, size: number): IObjectDefinition {
return {
name: name,
size: size,
parse: Number,
toFixedString: (field, value) => {
const strVal = value && value.toString ? value.toString() : '0'
const pad = new Array(Math.max(0, field.size + 1 - strVal.length)).join(
'0'
)
return pad + strVal
}
}

public static strTrunc(name: string, size: number): IObjectDefinition {
return {
name: name,
size: size,
toFixedString: (field, value) => {
const str = (value || '').substring(0, size)
return textToString(field, str)
}
}
export function stringTruncObject(
name: string,
size: number
): IObjectDefinition {
return {
name: name,
size: size,
toFixedString: (field, value) => {
const str = (value || '').substring(0, size)
return textToString(field, str)
}
}

public static number(name: string, size: number): IObjectDefinition {
return {
name: name,
size: size,
parse: Number,
toFixedString: (field, value) => {
const strVal = value && value.toString ? value.toString() : '0'
const pad = new Array(Math.max(0, field.size + 1 - strVal.length)).join(
'0'
}
export function curriedObjectify(objectDefinitions: IObjectDefinition[]) {
return <TargetObject>(str: string = ''): TargetObject => {
let from = 0
str = str || ''
return objectDefinitions.reduce(
(obj, field) => {
const parse = field.parse || parseText
obj[field.name] = parse(str.substring(from, from + field.size))
from += field.size
return obj
},
{} as TargetObject
)
}
}
export function curriedStringify(objectDefinitions: IObjectDefinition[]) {
return (obj: any) => {
return objectDefinitions.reduce((str, field) => {
const toStr = field.toFixedString || textToString
const strValue = toStr(field, obj[field.name])
if (strValue && strValue.length > field.size) {
throw new Error(
'truncation error on field: ' +
field.name +
', size: ' +
field.size +
', value: ' +
obj[field.name]
)
return pad + strVal
}
}
return str + strValue
}, '')
}
}
export function createFixedStr(objectDefinitions: IObjectDefinition[]) {
return {
objectify: curriedObjectify(objectDefinitions),
stringify: curriedStringify(objectDefinitions)
}
}
export class FixedStr implements IFixedStr {
public static str = stringObject

private objDef: IObjectDefinition[]
public static strTrunc = stringTruncObject

public static number = numberObject

private objDef: IObjectDefinition[]
constructor(ObjectDefinitions: IObjectDefinition[]) {
this.objDef = ObjectDefinitions
}
Expand Down
Loading