Skip to content

Commit

Permalink
deprecate hasOwnProperty in favour of has
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Mar 2, 2021
1 parent de69ce5 commit 281dc29
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 28 deletions.
12 changes: 8 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ high state of flux, you're at risk of it changing without notice.
- `Applicative`
- deprecate `getApplicativeComposition`, use `ap` helper instead
- `Array`
- deprecate `prependToAll`, use `prependAll`
- deprecate `prependToAll`, use `prependAll` instead
- `BooleanAlgebra`
- deprecate `booleanAlgebraBoolean`, use `boolean.BooleanAlgebra` instead
- deprecate `getFunctionBooleanAlgebra`, use `function.getBooleanAlgebra` instead
Expand Down Expand Up @@ -88,7 +88,7 @@ high state of flux, you're at risk of it changing without notice.
- deprecate `getTupleMonoid`, use `tuple` instead
- `NonEmptyArray`
- deprecate `fold`, use `concatAll` instead
- deprecate `prependToAll`, use `prependAll`
- deprecate `prependToAll`, use `prependAll` instead
- `Option`
- deprecate `getApplySemigroup` in favour of `Apply.getApplySemigroup`
- deprecate `getApplyMonoid` in favour of `Applicative.getApplicativeMonoid`
Expand Down Expand Up @@ -128,10 +128,14 @@ high state of flux, you're at risk of it changing without notice.
- `ReaderTaskEither`
- deprecate `run`
- `ReadonlyArray`
- deprecate `prependToAll`, use `prependAll`
- deprecate `prependToAll`, use `prependAll` instead
- `ReadonlyNonEmptyArray`
- deprecate `fold`, use `concatAll` instead
- deprecate `prependToAll`, use `prependAll`
- deprecate `prependToAll`, use `prependAll` instead
- `ReadonlyRecord`
- deprecate `hasOwnProperty`, use `has` instead
- `Record`
- deprecate `hasOwnProperty`, use `has` instead
- `Ring`
- deprecate `getTupleRing`, use `tuple` instead
- `Semigroup`
Expand Down
25 changes: 21 additions & 4 deletions docs/modules/ReadonlyRecord.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Added in v2.5.0
- [foldMapWithIndex](#foldmapwithindex)
- [fromFoldable](#fromfoldable)
- [fromFoldableMap](#fromfoldablemap)
- [hasOwnProperty (function)](#hasownproperty-function)
- [has](#has)
- [isEmpty](#isempty)
- [isSubrecord](#issubrecord)
- [keys](#keys)
Expand All @@ -86,6 +86,7 @@ Added in v2.5.0
- [traverse](#traverse)
- [traverseWithIndex](#traversewithindex)
- [updateAt](#updateat)
- [~~hasOwnProperty (function)~~](#hasownproperty-function)

---

Expand Down Expand Up @@ -744,15 +745,19 @@ assert.deepStrictEqual(

Added in v2.5.0

## hasOwnProperty (function)
## has

Test whether or not a key exists in a `ReadonlyRecord`.

Note. This function is not pipeable because is a custom type guard.

**Signature**

```ts
export declare function hasOwnProperty<K extends string>(k: string, r: ReadonlyRecord<K, unknown>): k is K
export declare const has: <K extends string>(k: string, r: Readonly<Record<K, unknown>>) => k is K
```

Added in v2.5.0
Added in v2.10.0

## isEmpty

Expand Down Expand Up @@ -1021,3 +1026,15 @@ export declare function updateAt<A>(
```

Added in v2.5.0

## ~~hasOwnProperty (function)~~

Use `has` instead.

**Signature**

```ts
export declare function hasOwnProperty<K extends string>(k: string, r: ReadonlyRecord<K, unknown>): k is K
```

Added in v2.5.0
25 changes: 21 additions & 4 deletions docs/modules/Record.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Added in v2.0.0
- [getEq](#geteq)
- [getMonoid](#getmonoid)
- [getShow](#getshow)
- [hasOwnProperty (function)](#hasownproperty-function)
- [has](#has)
- [insertAt](#insertat)
- [isEmpty](#isempty)
- [isSubrecord](#issubrecord)
Expand All @@ -80,6 +80,7 @@ Added in v2.0.0
- [traverse](#traverse)
- [traverseWithIndex](#traversewithindex)
- [updateAt](#updateat)
- [~~hasOwnProperty (function)~~](#hasownproperty-function)

---

Expand Down Expand Up @@ -611,15 +612,19 @@ export declare const getShow: <A>(S: Show<A>) => Show<Record<string, A>>

Added in v2.0.0

## hasOwnProperty (function)
## has

Test whether or not a key exists in a `Record`.

Note. This function is not pipeable because is a custom type guard.

**Signature**

```ts
export declare const hasOwnProperty: <K extends string>(k: string, r: Record<K, unknown>) => k is K
export declare const has: <K extends string>(k: string, r: Record<K, unknown>) => k is K
```

Added in v2.0.0
Added in v2.10.0

## insertAt

Expand Down Expand Up @@ -947,3 +952,15 @@ export declare const updateAt: <A>(k: string, a: A) => <K extends string>(r: Rec
```

Added in v2.0.0

## ~~hasOwnProperty (function)~~

Use `has` instead.

**Signature**

```ts
export declare const hasOwnProperty: <K extends string>(k: string, r: Record<K, unknown>) => k is K
```

Added in v2.0.0
27 changes: 19 additions & 8 deletions src/ReadonlyRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@ export function insertAt<A>(k: string, a: A): (r: ReadonlyRecord<string, A>) =>

const _hasOwnProperty = Object.prototype.hasOwnProperty

// TODO: rename in v3 to avoid #1249
/**
* @since 2.5.0
* Test whether or not a key exists in a `ReadonlyRecord`.
*
* Note. This function is not pipeable because is a custom type guard.
*
* @since 2.10.0
*/
export function hasOwnProperty<K extends string>(k: string, r: ReadonlyRecord<K, unknown>): k is K
export function hasOwnProperty<K extends string>(this: any, k: string, r?: ReadonlyRecord<K, unknown>): k is K {
return _hasOwnProperty.call(r === undefined ? this : r, k)
}
export const has = <K extends string>(k: string, r: ReadonlyRecord<K, unknown>): k is K => _hasOwnProperty.call(r, k)

/**
* Delete a key and value from a map
Expand Down Expand Up @@ -197,7 +197,7 @@ export function updateAt<A>(
a: A
): <K extends string>(r: ReadonlyRecord<K, A>) => Option<ReadonlyRecord<K, A>> {
return <K extends string>(r: ReadonlyRecord<K, A>) => {
if (!hasOwnProperty(k, r)) {
if (!has(k, r)) {
return none
}
if (r[k] === a) {
Expand All @@ -217,7 +217,7 @@ export function modifyAt<A>(
f: (a: A) => A
): <K extends string>(r: ReadonlyRecord<K, A>) => Option<ReadonlyRecord<K, A>> {
return <K extends string>(r: ReadonlyRecord<K, A>) => {
if (!hasOwnProperty(k, r)) {
if (!has(k, r)) {
return none
}
const out: Record<K, A> = Object.assign({}, r)
Expand Down Expand Up @@ -1229,6 +1229,17 @@ export const Witherable: Witherable1<URI> = {
// deprecated
// -------------------------------------------------------------------------------------

/**
* Use `has` instead.
*
* @since 2.5.0
* @deprecated
*/
export function hasOwnProperty<K extends string>(k: string, r: ReadonlyRecord<K, unknown>): k is K
export function hasOwnProperty<K extends string>(this: any, k: string, r?: ReadonlyRecord<K, unknown>): k is K {
return _hasOwnProperty.call(r === undefined ? this : r, k)
}

/**
* Use small, specific instances instead.
*
Expand Down
16 changes: 14 additions & 2 deletions src/Record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,13 @@ export function insertAt<A>(k: string, a: A): (r: Record<string, A>) => Record<s
}

/**
* @since 2.0.0
* Test whether or not a key exists in a `Record`.
*
* Note. This function is not pipeable because is a custom type guard.
*
* @since 2.10.0
*/
export const hasOwnProperty: <K extends string>(k: string, r: Record<K, unknown>) => k is K = RR.hasOwnProperty
export const has: <K extends string>(k: string, r: Record<K, unknown>) => k is K = RR.has

/**
* Delete a key and value from a map
Expand Down Expand Up @@ -783,6 +787,14 @@ export const Witherable: Witherable1<URI> = {
// deprecated
// -------------------------------------------------------------------------------------

/**
* Use `has` instead.
*
* @since 2.0.0
* @deprecated
*/
export const hasOwnProperty: <K extends string>(k: string, r: Record<K, unknown>) => k is K = RR.has

/**
* Use small, specific instances instead.
*
Expand Down
11 changes: 8 additions & 3 deletions test/ReadonlyRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,17 @@ describe('ReadonlyRecord', () => {
U.deepStrictEqual(_.singleton('a', 1), { a: 1 })
})

it('hasOwnProperty', () => {
it('has', () => {
const x: _.ReadonlyRecord<string, number> = { a: 1 }
U.deepStrictEqual(_.hasOwnProperty('a', x), true)
U.deepStrictEqual(_.hasOwnProperty('b', x), false)
U.deepStrictEqual(_.has('a', x), true)
U.deepStrictEqual(_.has('b', x), false)
// TODO: remove in v3
// #1249
// tslint:disable-next-line: deprecation
U.deepStrictEqual(_.hasOwnProperty('a', x), true)
// tslint:disable-next-line: deprecation
U.deepStrictEqual(_.hasOwnProperty('b', x), false)
// tslint:disable-next-line: deprecation
const hasOwnProperty: any = _.hasOwnProperty
U.deepStrictEqual(hasOwnProperty.call(x, 'a'), true)
U.deepStrictEqual(hasOwnProperty.call(x, 'b'), false)
Expand Down
6 changes: 3 additions & 3 deletions test/Record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,10 @@ describe('Record', () => {
U.deepStrictEqual(_.singleton('a', 1), { a: 1 })
})

it('hasOwnProperty', () => {
it('has', () => {
const x: Record<string, number> = { a: 1 }
U.deepStrictEqual(_.hasOwnProperty('a', x), true)
U.deepStrictEqual(_.hasOwnProperty('b', x), false)
U.deepStrictEqual(_.has('a', x), true)
U.deepStrictEqual(_.has('b', x), false)
})

it('updateAt', () => {
Expand Down

0 comments on commit 281dc29

Please sign in to comment.