https://github.com/nju33/abject
Get property value from an object by alias name
https://github.com/nju33/abject
aliases object
Last synced: 4 months ago
JSON representation
Get property value from an object by alias name
- Host: GitHub
- URL: https://github.com/nju33/abject
- Owner: nju33
- Created: 2018-03-07T02:50:11.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-07T09:34:22.000Z (over 7 years ago)
- Last Synced: 2025-01-23T00:22:17.216Z (6 months ago)
- Topics: aliases, object
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/abject
- Size: 47.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# abject
Get property value from an object by alias name
[](https://www.npmjs.com/package/abject)
[](https://circleci.com/gh/nju33/abject)
[](https://coveralls.io/github/nju33/abject?branch=master)
[](https://github.com/facebook/jest)
[](https://github.com/prettier/prettier)

[](https://github.com/ellerbrock/typescript-badges/)## Install
```bash
yarn add [-D] abject
```## Usage
```ts
import abject = require('./abject');const post = {
post: {
id: 1,
title: 'title',
url: 'https://example.com',
category: {
id: 1,
name: 'hoge'
},
tags: [
{
id: 1,
name: 'foo',
},
{
id: 2,
name: 'bar',
}
]
}
}
interface IdealPost {
id: number
title: string,
url: string,
category: string,
tags: [string, string]
}const idealPostAbject = abject as abject.Fn;
/**
* Register an alias with its actual path
* `{ alias: actualPath }`
*/
const idealPost = idealPostAbject(post, {
id: 'post.id',
title: 'post.title',
url: 'post.url',
category: 'post.category.name',
tags: 'post.tags[].name'
});const id = idealPost('id')
// const id: number
expect(id).toBe(1);const category = idealPost('category');
// const category: string
expect(category).toBe('hoge');const tags = idealPost('tags');
// const tags: [string, string]
expect(tags).toEqual(
expect.arrayContaining(['foo', 'bar'])
);
```