Skip to content

Commit

Permalink
fix(types): Add missing undefined return value (#2505)
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 authored Apr 30, 2022
1 parent 16dbf20 commit 7d32583
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/api/attributes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('$(...)', () => {
describe('.attr', () => {
it('() : should get all the attributes', () => {
const attrs = $('ul').attr();
expect(attrs.id).toBe('fruits');
expect(attrs).toHaveProperty('id', 'fruits');
});

it('(invalid key) : invalid attr should get undefined', () => {
Expand Down Expand Up @@ -69,9 +69,9 @@ describe('$(...)', () => {
'data-url': 'http://apple.com',
});
const attrs = $('.apple').attr();
expect(attrs.id).toBe('apple');
expect(attrs.style).toBe('color:red;');
expect(attrs['data-url']).toBe('http://apple.com');
expect(attrs).toHaveProperty('id', 'apple');
expect(attrs).toHaveProperty('style', 'color:red;');
expect(attrs).toHaveProperty('data-url', 'http://apple.com');
});

it('(map, val) : should throw with wrong combination of arguments', () => {
Expand All @@ -95,7 +95,7 @@ describe('$(...)', () => {
return 'ninja';
});
const attrs = $fruits.attr();
expect(attrs.id).toBe('ninja');
expect(attrs).toHaveProperty('id', 'ninja');
});

it('(key, function) : should ignore text nodes', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/api/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function getAttr(
elem: AnyNode,
name: undefined,
xmlMode?: boolean
): Record<string, string>;
): Record<string, string> | undefined;
function getAttr(
elem: AnyNode,
name: string,
Expand Down Expand Up @@ -138,7 +138,7 @@ export function attr<T extends AnyNode>(
*/
export function attr<T extends AnyNode>(
this: Cheerio<T>
): Record<string, string>;
): Record<string, string> | undefined;

/**
* Method for setting attributes. Sets the attribute value for only the first
Expand Down
10 changes: 7 additions & 3 deletions src/api/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { Cheerio } from '../cheerio';
export function css<T extends AnyNode>(
this: Cheerio<T>,
names?: string[]
): Record<string, string>;
): Record<string, string> | undefined;
/**
* Get the value of a style property for the first element in the set of matched elements.
*
Expand Down Expand Up @@ -75,6 +75,10 @@ export function css<T extends AnyNode>(
});
}

if (this.length === 0) {
return undefined;
}

return getCss(this[0], prop as string);
}

Expand Down Expand Up @@ -125,7 +129,7 @@ function setCss(
* @param props - Optionally the names of the properties of interest.
* @returns The parsed styles.
*/
function getCss(el?: AnyNode, props?: string[]): Record<string, string>;
function getCss(el: AnyNode, props?: string[]): Record<string, string>;
/**
* Get a property from the parsed styles of the first element.
*
Expand All @@ -137,7 +141,7 @@ function getCss(el?: AnyNode, props?: string[]): Record<string, string>;
*/
function getCss(el: AnyNode, prop: string): string | undefined;
function getCss(
el?: AnyNode,
el: AnyNode,
prop?: string | string[]
): Record<string, string> | string | undefined {
if (!el || !isTag(el)) return;
Expand Down

0 comments on commit 7d32583

Please sign in to comment.