小编Wil*_*nik的帖子

assertAll与JUnit5中的多个断言

是否有任何理由对多个断言进行分组:

public void shouldTellIfPrime(){
    Assertions.assertAll(
            () -> assertTrue(isPrime(2)),
            () -> assertFalse(isPrime(4))
    );
}
Run Code Online (Sandbox Code Playgroud)

而不是这样做:

public void shouldTellIfPrime(){
    Assertions.assertTrue(isPrime(2));
    Assertions.assertFalse(isPrime(4));
}
Run Code Online (Sandbox Code Playgroud)

java junit unit-testing assertions junit5

28
推荐指数
2
解决办法
1万
查看次数

访问自定义表单控件的有效值

我创建了代表密码表单控件的自定义组件(简化了以下代码).

PasswordComponent(html)

<form [formGroup]="passwordForm">
  ...
  <input formControlName="password" type="password">
</form>
Run Code Online (Sandbox Code Playgroud)

PasswordComponent(ts)

...
@Component({
  selector: 'password',
  templateUrl: './password.component.html',
  styleUrls: ['./password.component.css'],
  providers: [{
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => PasswordComponent),
    multi: true
  }]
})
export class PasswordComponent implements ControlValueAccessor {

  passwordForm: FormGroup;
  onChange = (password: string) => { };  
  onTouched = () => { };                  

  constructor() {
    this.passwordForm = new FormGroup({
      ...
      password: new FormControl('')
    });

    this.passwordForm.valueChanges.subscribe(data => this.onChange(this.value));
  }

  get value(): string {
    return this.passwordForm.get('password').value;
  }

  writeValue(password: string): void {
    this.passwordForm.get('password').setValue(password);
    this.onChange(this.value);
  } …
Run Code Online (Sandbox Code Playgroud)

validation angular2-forms angular

8
推荐指数
1
解决办法
2498
查看次数

从函数参数推断泛型类型之一

考虑以下示例。fetchItems函数根据传递的onlyBody参数返回响应或响应正文,默认为true.

interface HttpResponse<T> {
  body: T
}

function fetchItems<T, B extends boolean>(url: string, onlyBody: B = true as B) {
  return Promise
    .resolve({body: 'some data'} as any)
    .then<B extends true ? T : HttpResponse<T>>(res => onlyBody ? res.body : res);
}
Run Code Online (Sandbox Code Playgroud)

如果传递了两种泛型类型,则该函数将按预期工作

const a = fetchItems<string, false>('url', false) // Promise<HttpResponse<string>>
const b = fetchItems<string, true>('url', true)   // Promise<string>
const c = fetchItems<string, true>('url')         // Promise<string>
Run Code Online (Sandbox Code Playgroud)

我想放弃传递B类型的要求,因为它相对于onlyBody参数来说是多余的。但是当B类型没有显式传递时,ts 编译器会抱怨它(预期有 2 …

typescript

3
推荐指数
1
解决办法
954
查看次数

为什么 '?'[0] === '?' 但是 '??'[0] !== '??'?

'\xe2\x9d\x8c'[0] === '\xe2\x9d\x8c' // true\n'\xe2\x9c\x94\xef\xb8\x8f'[0] === '\xe2\x9c\x94\xef\xb8\x8f' // false\n'\xe2\x9c\x94\xef\xb8\x8f'[0] === '\xe2\x9c\x94'  // true\n\n
Run Code Online (Sandbox Code Playgroud)\n

我怀疑它与 unicode 相关,但想准确了解发生了什么以及如何正确比较这些字符。为什么 '\xe2\x9c\x94\xef\xb8\x8f' 的处理方式与 '\xe2\x9d\x8c' 不同?

\n

我在这个简单的字符计数中遇到了它

\n
'\xe2\x9c\x94\xef\xb8\x8f\xe2\x9d\x8c\xe2\x9c\x94\xef\xb8\x8f\xe2\x9d\x8c'.split('').filter(e => e === '\xe2\x9d\x8c').length // 2\n'\xe2\x9c\x94\xef\xb8\x8f\xe2\x9d\x8c\xe2\x9c\x94\xef\xb8\x8f\xe2\x9d\x8c'.split('').filter(e => e === '\xe2\x9c\x94\xef\xb8\x8f').length // 0\n
Run Code Online (Sandbox Code Playgroud)\n

javascript string unicode

3
推荐指数
1
解决办法
373
查看次数

具有不同高度的子行(嵌套单元)的角材料表

我想用嵌套单元格模拟子行创建一个表。如果每个单元格的内容不超过嵌套单元格(A)的min-height属性值,则该方法工作正常。

不幸的是,当超过此值时,表会崩溃(B)。有没有简单的方法可以正确对齐嵌套单元格以获得像(C)这样的表?

代码:https//stackblitz.com/edit/angular-aejwfm

img

(*)我知道我们可以使用javascript将代表某个子行的所有单元格的高度设置为max(该子行中所有单元格的高度),但是我更喜欢纯HTML / CSS / Angular解决方案。

css css3 flexbox angular-material angular

1
推荐指数
1
解决办法
7319
查看次数