2.0.0 • Published 8 months ago

@awerlogus/data-types v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Useful data types for javascript

Installation

Run from command line

npm i @awerlogus/data-types

Usage example

const N = require('@awerlogus/data-types/lib/nullable')
const O = require('@awerlogus/data-types/lib/option')
const E = require('@awerlogus/data-types/lib/either')
const J = require('@awerlogus/data-types/lib/json')

/** @type {(data: J.Json) => data is ReadonlyArray<J.Json>} */
const isArray = Array.isArray

/** @type {(data: unknown) => data is Object} */
const isObject = data => typeof data === 'object'

/** @type {(data: unknown) => data is string} */
const isString = data => typeof data === 'string'

/** @type {(data: J.Json) => data is Record<string, J.Json>} */
const isExactObject = data => isObject(data) && !isArray(data) && N.isSome(data)

/** @type {(data: string) => E.Either<string, string>} */
const handleInput = rawData => {
  const data = J.decode(rawData)

  if (O.isNone(data)) {
    return E.left('Data is not a valid json')
  }

  if (!isExactObject(data)) {
    return E.left('Data is not an object')
  }

  const value = data.key

  if (O.isNone(value)) {
    return E.left('Property \'key\' not found in the object')
  }

  if (!isString(value)) {
    return E.left('Property \'key\' must be a string')
  }

  return E.right(value)
}

const input = '{ "key": "value" }'

const result = handleInput(input)

// If result is a Left value, throws an error
// If result is a Right value, logs it
console.log(E.extractUnsafe(result))
1.6.4

8 months ago

1.5.4

8 months ago

1.4.4

9 months ago

1.3.4

10 months ago

1.3.3

10 months ago

2.0.0

8 months ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.9

1 year ago

1.2.8

1 year ago

1.2.7

1 year ago

1.2.6

1 year ago

1.2.5

2 years ago

1.2.0

2 years ago

1.1.9

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago