Skip to content

Commit

Permalink
feat@method underscore proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
selfrefactor committed Apr 14, 2019
1 parent 6151a4a commit 5cbad23
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions files/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# CHANGELOG

2.0.0 `R._`

2.0.0 `R.toggle`

1.9.0 `R.pushUniq`
Expand Down
11 changes: 11 additions & 0 deletions files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,17 @@ const result = truncate('12345678')

[Source](https://github.com/selfrefactor/rambdax/tree/master/src/when.js)

#### _

> _ : object
A proxy object which always returns the target property.

```
console.log(_.FOO) // => 'FOO'
console.log(_.BAR) // => 'BAR'
```

#### whenAsync

> whenAsync<T>(rule: condition: Async | Function | boolean, whenFn: Async | Function): Promise<T>
Expand Down
10 changes: 10 additions & 0 deletions files/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ declare namespace R {
is: isfn<Switchem<T>>
default: IdentityFunction<T>
}

interface UnderscoreBase {
[prop: string]: string,
}
interface UnderscoreFn {
['STATUS']: (status: string|boolean) => string,
}
type Underscore = UnderscoreBase | UnderscoreFn
// RAMBDAX_END
// RAMDA_START
type Ord = number | string | boolean
Expand Down Expand Up @@ -164,6 +172,8 @@ declare namespace R {
// RAMDA_END
interface X {
// RAMBDAX_START
_: Underscore

allFalse(...input: Array<any>): boolean
anyFalse(...input: Array<any>): boolean

Expand Down
1 change: 1 addition & 0 deletions rambdax.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './src/_'
export * from './src/allFalse'
export * from './src/allTrue'
export * from './src/allType'
Expand Down
22 changes: 22 additions & 0 deletions src/_.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const predefined = {
STATUS : x => {
const flag = typeof x === 'boolean' ?
() => x :
() => [ 'ok', 'pass', 'passed' ].includes(x.toLowerCase())

return flag() ?
'OK' :
'FAIL'
},
}

const handler = {
get : function(target, property){
if (target[ property ]) return target[ property ]

return property
},
}

export const _ = new Proxy(predefined, handler)

31 changes: 31 additions & 0 deletions src/_.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { _ } from './_'

test('', () => {
const result = _.MORE

expect(
result
).toEqual('MORE')
expect(
result
).toEqual(_.MORE)
})

test('with status', () => {
expect(
_.STATUS('ok')
).toEqual('OK')
expect(
_.STATUS(true)
).toEqual('OK')
expect(
_.STATUS('no')
).toEqual('FAIL')
expect(
_.STATUS(false)
).toEqual('FAIL')
expect(
_.STATUS('pass')
).toEqual('OK')
})

0 comments on commit 5cbad23

Please sign in to comment.