refactor(aio): clean up `attribute-utils` (#23960)

PR Close #23960
This commit is contained in:
George Kalpakas 2018-05-17 13:03:01 +03:00 committed by Matias Niemelä
parent 6e7d071c6b
commit a7b07defe1
2 changed files with 27 additions and 27 deletions

View File

@ -1,6 +1,6 @@
import { ElementRef } from '@angular/core';
import { getAttrs, getAttrValue, getBoolFromAttribute, boolFromValue } from './attribute-utils';
import { AttrMap, getAttrs, getAttrValue, getBoolFromAttribute, boolFromValue } from './attribute-utils';
describe('Attribute Utilities', () => {
let testEl: HTMLElement;
@ -32,17 +32,17 @@ describe('Attribute Utilities', () => {
});
describe('getAttrValue', () => {
let attrMap: { [index: string]: string };
let attrMap: AttrMap;
beforeEach(() => {
attrMap = getAttrs(testEl);
});
it('should return empty string value for attribute "a"', () => {
it('should return empty string for attribute "a"', () => {
expect(getAttrValue(attrMap, 'a')).toBe('');
});
it('should return empty string value for attribute "A"', () => {
it('should return empty string for attribute "A"', () => {
expect(getAttrValue(attrMap, 'A')).toBe('');
});
@ -50,7 +50,7 @@ describe('Attribute Utilities', () => {
expect(getAttrValue(attrMap, 'b')).toBe('true');
});
it('should return empty string value for attribute "d-E"', () => {
it('should return empty string for attribute "d-E"', () => {
expect(getAttrValue(attrMap, 'd-E')).toBe('');
});
@ -68,12 +68,10 @@ describe('Attribute Utilities', () => {
expect(getAttrValue(attrMap, ['d-e', 'd'])).toBe('');
});
it('should return undefined value for non-existent attribute "x"', () => {
it('should return undefined for non-existent attributes', () => {
expect(getAttrValue(attrMap, 'x')).toBeUndefined();
});
it('should return undefined if no argument', () => {
expect(getAttrValue(attrMap)).toBeUndefined();
expect(getAttrValue(attrMap, '')).toBeUndefined();
expect(getAttrValue(attrMap, ['', 'x'])).toBeUndefined();
});
});

View File

@ -1,17 +1,19 @@
// Utilities for processing HTML element attributes
import { ElementRef } from '@angular/core';
interface StringMap { [index: string]: string; }
export interface AttrMap {
[key: string]: string;
}
/**
* Get attribute map from element or ElementRef `attributes`.
* Attribute map keys are forced lowercase for case-insensitive lookup.
* @param el The source of the attributes
* @param el The source of the attributes.
*/
export function getAttrs(el: HTMLElement | ElementRef): StringMap {
export function getAttrs(el: HTMLElement | ElementRef): AttrMap {
const attrs: NamedNodeMap = el instanceof ElementRef ? el.nativeElement.attributes : el.attributes;
const attrMap: StringMap = {};
for (const attr of attrs as any /* cast due to https://github.com/Microsoft/TypeScript/issues/2695 */) {
const attrMap: AttrMap = {};
for (const attr of attrs as any as Attr[] /* cast due to https://github.com/Microsoft/TypeScript/issues/2695 */) {
attrMap[attr.name.toLowerCase()] = attr.value;
}
return attrMap;
@ -19,29 +21,29 @@ export function getAttrs(el: HTMLElement | ElementRef): StringMap {
/**
* Return the attribute that matches `attr`.
* @param attr Name of the attribute or a string of candidate attribute names
* @param attr Name of the attribute or a string of candidate attribute names.
*/
export function getAttrValue(attrs: StringMap, attr: string | string[] = ''): string {
return attrs[typeof attr === 'string' ?
attr.toLowerCase() :
attr.find(a => attrs[a.toLowerCase()] !== undefined) || ''
];
export function getAttrValue(attrs: AttrMap, attr: string | string[]): string | undefined {
const key = (typeof attr === 'string')
? attr
: attr.find(a => attrs.hasOwnProperty(a.toLowerCase()));
return (key === undefined) ? undefined : attrs[key.toLowerCase()];
}
/**
* Return the boolean state of an attribute value (if supplied)
* @param attrValue The string value of some attribute (or undefined if attribute not present)
* @param attrValue The string value of some attribute (or undefined if attribute not present).
* @param def Default boolean value when attribute is undefined.
*/
export function boolFromValue(attrValue: string | undefined, def: boolean = false) {
// tslint:disable-next-line:triple-equals
return attrValue == undefined ? def : attrValue.trim() !== 'false';
return attrValue === undefined ? def : attrValue.trim() !== 'false';
}
/**
* Return the boolean state of attribute from an element
* @param el The source of the attributes
* @param atty Name of the attribute or a string of candidate attribute names
* @param el The source of the attributes.
* @param atty Name of the attribute or a string of candidate attribute names.
* @param def Default boolean value when attribute is undefined.
*/
export function getBoolFromAttribute(