diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..7799c46 --- /dev/null +++ b/.github/workflows/release.yml @@ -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}} \ No newline at end of file diff --git a/README.md b/README.md index ea82c1d..2bdeb01 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ yarn add oproxy - [API](#api) - [`oproxy`](#oproxysrc-object-schema-schema) + - [`array`](#oproxysrc-object-schema-schema) ## API @@ -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) \ No newline at end of file diff --git a/package.json b/package.json index 2a10d4b..bde9105 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/index.ts b/src/index.ts index 52544ef..334aa56 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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(src: T, schema: Schema) { diff --git a/src/plugins/array/README.md b/src/plugins/array/README.md new file mode 100644 index 0000000..f91e851 --- /dev/null +++ b/src/plugins/array/README.md @@ -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'] } +``` diff --git a/src/plugins/core.ts b/src/plugins/core.ts index b53cb63..d17f1dc 100644 --- a/src/plugins/core.ts +++ b/src/plugins/core.ts @@ -1,8 +1,8 @@ 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; @@ -10,6 +10,6 @@ export class Core { enqueue(name: string, fn: any) { this.tasks.set(name, fn); - return this + return this; } } diff --git a/src/plugins/string/index.ts b/src/plugins/string/index.ts index 207be6a..cc56acf 100644 --- a/src/plugins/string/index.ts +++ b/src/plugins/string/index.ts @@ -1,6 +1,5 @@ import { StringPlugins } from './plugins'; - export function string(key: string): StringPlugins { return new StringPlugins(key); } diff --git a/src/plugins/string/plugins.ts b/src/plugins/string/plugins.ts index 34d079e..eb4fa71 100644 --- a/src/plugins/string/plugins.ts +++ b/src/plugins/string/plugins.ts @@ -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(); }); diff --git a/src/utils/isExpr.ts b/src/utils/isExpr.ts index d70a38e..578f6f9 100644 --- a/src/utils/isExpr.ts +++ b/src/utils/isExpr.ts @@ -1,4 +1,4 @@ -const regex = /[^{\}]+(?=})/g; +const regex = /[^{}]+(?=})/g; export function isExpr(value: any): boolean { return regex.test(value); diff --git a/src/utils/types.ts b/src/utils/types.ts index beec43c..fc44aea 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -1 +1 @@ -export type Maybe = T | null | undefined; \ No newline at end of file +export type Maybe = T | null | undefined; diff --git a/yarn.lock b/yarn.lock index f870e3a..8f819a8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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"