我在Python 3.4中使用捷克语重音文本.
调用re.sub()正则表达式对重音句子执行替换效果很好,但是使用正则表达式编译re.compile()然后调用regex.sub()失败.
在这种情况下,我使用相同的参数re.sub()和regex.sub()
import re
pattern = r'(?<!\*)(Poplatn[ií]\w+ da[n?]\w+)'
flags = re.I|re.L
compiled = re.compile(pattern, flags)
text = 'Poplatníkem dan? z pozemk? je vlastník pozemku'
mark = r'**\1**' # wrap 1st matching group in double stars
print(re.sub(pattern, mark, text, flags))
# outputs: **Poplatníkem dan?** z pozemk? je vlastník pozemku
# substitution works
print(compiled.sub(mark, text))
# outputs: Poplatníkem dan? z pozemk? je vlastník pozemku
# substitution fails
Run Code Online (Sandbox Code Playgroud)
我认为原因是口音,因为对于非重音句re.sub() …
我正在使用拖放式API,有两种方法可以从a收集文件DragEvent.dataTransfer,有readonly files: FileList和readonly items: DataTransferItemList.
它似乎items是超集files,收集File[]来自items更复杂,并且items在旧的IE中也不受支持,因此files更容易使用并且具有更好的支持,但是MDN上的文章items首先使用并且仅当它不受支持时才切换到files.
我的问题是,如果我只需要收集File[]收藏品DragEvent,我可以选择dataTransfer.files房产还是dataTransfer.items有一些好处,这值得吗?
在现代浏览器(chrome)中,files集合为空.拖动的文件只能通过items,因此您不能单独依赖files.
我有 TypeScript ES6 类,假设它看起来像这样
class Classic
private _member: Object;
constructor(member: Object) {
this._member = member;
}
}
Run Code Online (Sandbox Code Playgroud)
我有一些丰富的对象,其中包含很多我想继承的方法,但该对象不是 TS 或 ES6 类,它只是 POJO(ObjectLiteral)。
所以我做了快速而肮脏的继承,比如
Object.assign(Classic.prototype, ObjectLiteralWithMethods);
Run Code Online (Sandbox Code Playgroud)
不需要深复制,一切正常。
我如何告诉 TypeScriptClassic具有这些继承的方法并指定它们的行为?
我搜索了像声明或定义AND类AND方法这样的关键字,但我的google-fu还不够。
我尝试为 定义接口Classic,但它也不起作用。
我试图像这样声明类
declare class ObjectLiteralWithMethods {
public method(): void;
protected _method(parameter: string): void;
}
Run Code Online (Sandbox Code Playgroud)
然后扩展该声明,例如
class Classic extends ObjectLiteralWithMethods { ... }
Run Code Online (Sandbox Code Playgroud)
但随后 TypeScript 希望我调用super()in constructor ,但失败了。
我知道我可以在Classic类似的方法上实现“虚拟”方法
class Classic {
dummyMethodImplementation(param: string): string {
return param;
}
}
Run Code Online (Sandbox Code Playgroud)
但这将是痛苦且低效的。我只想定义/声明该类Classic …
在 TS 之前的世界中,我们曾经像这样将简单的 HOC 分配给 const。(代码使用recompose/mapProps)
const withAverage = mapProps(
(props) => ({ ...props, average: avg(props.numbers) })
)
Run Code Online (Sandbox Code Playgroud)
在TS世界中代码看起来像这样(它不是100%准确,但说明了问题)
const withAverage = <R extends DataRow, P extends WithData<R>>(
component: React.ComponentType<P>
): React.ComponentType<P & WithAverage<R>> =>
mapProps<P, P & WithAverage<R>>(
(props) => ({ ...props, average: avg(props.numbers) })
(component);
Run Code Online (Sandbox Code Playgroud)
TypeScript版本将HOC函数包装在另一个HOC函数中,但这不是必需的,mapProps已经提供了HOC所以只需
const withAverage = mapProps...
Run Code Online (Sandbox Code Playgroud)
就足够了,不需要将其包装在另一个函数中,例如
const withAverage = Component => mapProps(...)(Component)
Run Code Online (Sandbox Code Playgroud)
但据我所知,没有办法如何分配 HOC 来const支持泛型,比如
const withAverage<I,O> = mapProps<I, O>(...)
Run Code Online (Sandbox Code Playgroud)
泛型似乎仅支持函数声明,而不支持 HOC 赋值。还是我错了?
I encountered strange behavior of Array.prototype.includes in one edge case.
Given that Array.prototype.includes works on bound context, one might use it like this (which is working)
expect(Array.prototype.includes.call([1, 2], 1))).toBe(true)
Run Code Online (Sandbox Code Playgroud)
simply put, we bound array [1, 2] and test 1 for inclusion.
Then consider, that many Array.prototype methods are able to bound context to provided callback, so for example Array.prototype.some can be combined with Object.prototype.hasOwnProperty like this
expect(["foo", "bar"].some(Object.prototype.hasOwnProperty, { foo: 0 })).toBe(true)
Run Code Online (Sandbox Code Playgroud)
Here, .some accepts two parameters, (callback, [thisArg]), …
javascript ×2
typescript ×2
arrays ×1
es6-class ×1
generics ×1
node.js ×1
python ×1
python-3.x ×1
regex ×1
v8 ×1