Skip to content

Commit

Permalink
fix: regex searching object fix (type fixes too)
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidg3 committed Aug 24, 2020
1 parent 2d738b8 commit e3e4c08
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/AssertUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@ export class AssertUtils {
return get(object, path)
}

public static parseIncludeNeedle(needle: any) {
public static parseIncludeNeedle(
needle: any
): { needleHasArrayNotation: boolean; path?: string; expected?: any } {
const path = Object.keys(needle)[0]
const expected = needle[path]
const needleHasArrayNotation = path && path.search(/\[\]\./) > -1
const expected = path && needle[path]
const needleHasArrayNotation = !!(path && path.search(/\[\]\./) > -1)
return { needleHasArrayNotation, path, expected }
}

Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/assert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ export default class AssertTest extends AbstractSpruceTest {
[6, 7],
9
)
@test(
'include fails when searching an object for a regex',
{ foo: 'bar', taco: 'bravo' },
/yummy/
)
protected static doesIncludeThrowsAsExpected(
haystack: any,
needle: any,
Expand Down
5 changes: 3 additions & 2 deletions src/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ const spruceAssert: ISpruceAssert = {
isHaystackObject &&
isObjectLike(needle) &&
Object.keys(needle).length === 1 &&
!needleHasArrayNotation
!needleHasArrayNotation &&
path
) {
const actual = AssertUtils.valueAtPath(haystack, path)

Expand Down Expand Up @@ -296,7 +297,7 @@ const spruceAssert: ISpruceAssert = {
return
}

if (isHaystackObject && isObjectLike(needle)) {
if (isHaystackObject && isObjectLike(needle) && path) {
const {
actualBeforeArray,
pathAfterFirstArray,
Expand Down

0 comments on commit e3e4c08

Please sign in to comment.