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
22 changes: 22 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: "🚀 release"

on:
release:
types: [published]

jobs:
release:
name: 🚀 release
runs-on: ubuntu-latest
steps:
- name: 📚 checkout
uses: actions/checkout@v2.1.1
- name: 🟢 node
uses: actions/setup-node@v1.4.2
with:
node-version: 12
registry-url: https://registry.npmjs.org
- name: 🚀 publish
run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}}
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ yarn add oproxy

- [API](#api)
- [`oproxy`](#oproxysrc-object-schema-schema)
- [`array`](#oproxysrc-object-schema-schema)


## API
Expand All @@ -61,3 +62,25 @@ const schema = {
oproxy(src, schema);
// { username: 'fooBar'}
```


### array

Handles array based on schema.

```js
import oproxy, { array } from 'oproxy';

const src = {
users: ['foo', 'bar', 'baz', 'qux'],
};

const schema = {
list: array('users').map(item => item.toUpperCase()),
};

oproxy(src, schema);
// { list: ['FOO', 'BAR', 'BAZ', 'QUX'] }
```

[Full Documentation](/src/plugins/array/README.md)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@
"size-limit": "^7.0.8",
"tsdx": "^0.14.1",
"tslib": "^2.3.1",
"typescript": "^4.5.5"
"typescript": "^4.6.2"
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { isExpr } from './utils/isExpr';
import { get } from './utils/get';

export interface Schema {
[key: string]: any
[key: string]: any;
}

function run<T>(src: T, schema: Schema) {
Expand Down
119 changes: 119 additions & 0 deletions src/plugins/array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Array

Transforms array values into different shapes.

## array(key: string)

Gets the value of key and transform it into an array.

```js
import oproxy, { array } from 'oproxy';

const src = {
users: ['foo', 'bar', 'baz', 'qux'],
};

const schema = {
list: array('users'),
};

oproxy(src, schema);
// { list: ['foo', 'bar', 'baz', 'qux'] }
```

## Methods

### .chunk(size: number)

Returns an array of elements split into groups the length of size.

```js
import oproxy, { array } from 'oproxy';

const src = {
users: ['foo', 'bar', 'baz', 'qux'],
};

const schema = {
users: array('users').chunk(2),
};

oproxy(src, schema);
// { users: [['foo', 'bar'],['baz', 'qux']] }
```

### .compact()

Removes all falsy values from an array. In JavaScript, `false`, `null`, `0`, `""`, `undefined` and `NaN` are all falsy.

```js
import oproxy, { array } from 'oproxy';

const src = {
cities: ['Dublin', 'London', undefined, 'New York', false],
};

const schema = {
cities: array('cities').compact(),
};

oproxy(src, schema);
// { users: ['Dublin', 'London', 'New York'] }
```

### .drop(number= 1)

Drops first n elements. If n is not specified, it will drop the first element.

```js
import oproxy, { array } from 'oproxy';

const src = {
users: ['foo', 'bar', 'baz', 'qux'],
};

const schema = {
users: array('users').drop(2),
};

oproxy(src, schema);
// { users: ['baz', 'qux'] }
```

### .filter(fn: function)

Returns the elements of an array that meet the condition specified by a function. It's a wrapper of `Array.prototype.filter`.

```js
import oproxy, { array } from 'oproxy';

const src = {
users: ['foo', 'bar', 'baz', 'qux'],
};

const schema = {
users: array('users').filter(user => user !== 'bar'),
};

oproxy(src, schema);
// { users: ['foo', 'baz', 'qux'] }
```

### .sort(compareFn?: (firstElement: any, secondElement: any) => number)

Returns sorted array. It's using native `Array.prototype.sort` method. So you can pass a custom compare function.

```js
import oproxy, { array } from 'oproxy';

const src = {
users: ['foo', 'bar', 'baz', 'qux'],
};

const schema = {
users: array('users').sort(),
};

oproxy(src, schema);
// { users: ['bar', 'baz', 'foo', 'qux'] }
```
6 changes: 3 additions & 3 deletions src/plugins/core.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export class Core {
name = 'schema'
name = 'schema';
tasks = new Map();
key: string;

constructor(key: string) {
if (!key) throw new Error('O: key is required');
this.key = key;
}

enqueue(name: string, fn: any) {
this.tasks.set(name, fn);
return this
return this;
}
}
1 change: 0 additions & 1 deletion src/plugins/string/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { StringPlugins } from './plugins';


export function string(key: string): StringPlugins {
return new StringPlugins(key);
}
2 changes: 1 addition & 1 deletion src/plugins/string/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class StringPlugins extends Core {
* @example
* O('name').upperCase(); // 'VALUE'
*/
upperCase(): this {
upperCase(): this {
return this.enqueue('upperCase', (currentValue: string) => {
return currentValue.toUpperCase();
});
Expand Down
2 changes: 1 addition & 1 deletion src/utils/isExpr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const regex = /[^{\}]+(?=})/g;
const regex = /[^{}]+(?=})/g;

export function isExpr(value: any): boolean {
return regex.test(value);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type Maybe<T> = T | null | undefined;
export type Maybe<T> = T | null | undefined;
18 changes: 4 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1379,11 +1379,6 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
integrity sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw==

"@types/uuid@^8.3.4":
version "8.3.4"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.4.tgz#bd86a43617df0594787d38b735f55c805becf1bc"
integrity sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw==

"@types/yargs-parser@*":
version "20.2.1"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-20.2.1.tgz#3b9ce2489919d9e4fea439b76916abc34b2df129"
Expand Down Expand Up @@ -6278,10 +6273,10 @@ typescript@^3.7.3:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.10.tgz#70f3910ac7a51ed6bef79da7800690b19bf778b8"
integrity sha512-w6fIxVE/H1PkLKcCPsFqKE7Kv7QUwhU8qQY2MueZXWx5cPZdwFupLgKK3vntcK98BtNHZtAF4LA/yl2a7k8R6Q==

typescript@^4.5.5:
version "4.5.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.5.tgz#d8c953832d28924a9e3d37c73d729c846c5896f3"
integrity sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==
typescript@^4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.6.2.tgz#fe12d2727b708f4eef40f51598b3398baa9611d4"
integrity sha512-HM/hFigTBHZhLXshn9sN37H085+hQGeJHJ/X7LpBWLID/fbc2acUMfU+lGD98X81sKP+pFa9f0DZmCwB9GnbAg==

unbox-primitive@^1.0.1:
version "1.0.1"
Expand Down Expand Up @@ -6366,11 +6361,6 @@ uuid@^3.3.2:
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==

uuid@^8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==

v8-compile-cache@^2.0.3:
version "2.3.0"
resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.3.0.tgz#2de19618c66dc247dcfb6f99338035d8245a2cee"
Expand Down