为了根据区域设置格式化数字,有一个标准的 JavaScript API:Intl.NumberFormat
但是对于反向操作,将字符串解析为数字我找不到任何支持语言环境的标准 API:
真的没有 JavaScript 标准 API 可以根据语言环境将字符串解析为数字吗?
如果没有:是否有任何市场已建立的开源库可以这样做?
为了确定当前的语言环境,我找到了不同的方法:
Accept-Language)navigator.languageprocess.env另一方面,ECMAScript 国际化 API 将locales每个Intl构造函数的参数定义为可选:
如果
locales未提供或未定义参数,则使用运行时的默认语言环境。
因此,似乎应该有一种独立于浏览器和操作系统的方式来获取“运行时的默认语言环境”。
有没有比获取运行时的默认语言环境更直接的方法
new Intl.NumberFormat().resolvedOptions().locale
Run Code Online (Sandbox Code Playgroud)
?
问题JavaScript 如何/在哪里检测默认语言环境?不同,因为它要求实现检测默认语言环境(在浏览器主机中)。与此相反,我的问题不是关于实现,而是关于标准 API 的存在。
我定义了以下函数:
inline fun <T> T.tryTo(block: T.() -> Unit): T? {
try {
block()
} catch (ex: IllegalArgumentException) {
return this
}
return null
}
Run Code Online (Sandbox Code Playgroud)
目的是在对象上构建一系列尝试操作,例如:
val input: String = getInput();
input.tryTo /* treat as a file name and open the file */ {
Desktop.getDesktop().open(File(this))
}?.tryTo /* treat as a number */ {
try {
doSomethingWithTheNumber(parseInt(this))
} catch (ex: NumberFormatException) {
throw IllegalArgumentException()
}
}?.tryTo {
println("All options tried, none worked out. Don't know how to treat this input.")
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,效果很好。 …
虽然 pydantic 的 ORM 模式记录在此处,但遗憾的是没有使用别名的文档。
from_orm如果pydantic模型定义了别名如何使用?
如果存在别名,工厂似乎from_orm会忘记所有非别名。- 请参阅下面的错误消息和相应的代码。这是一个错误还是一个功能?
下面的代码片段意外失败并出现验证错误:
pydantic.error_wrappers.ValidationError:SimpleModel
ThreeWordsId
字段需要 1 个验证错误(type=value_error.missing)
from sqlalchemy import Column, String
from sqlalchemy.ext.declarative import declarative_base
from pydantic import BaseModel, Field
Base = declarative_base()
class SimpleOrm(Base):
__tablename__ = 'simples'
three_words_id = Column(String, primary_key=True)
class SimpleModel(BaseModel):
three_words_id: str = Field(..., alias="threeWordsId")
class Config:
orm_mode=True
simple_orm = SimpleOrm(three_words_id='abc')
simple_oops = SimpleModel.from_orm(simple_orm)
Run Code Online (Sandbox Code Playgroud) 如何使用 Quarkus JPA 声明复合密钥?
尝试@Id在@EntityQuarkus的类中使用多个注释,会导致错误:
Currently the @Id annotation can only be placed on a single field or method. Offending class is abc.model.Property
at io.quarkus.spring.data.deployment.generate.StockMethodsAdder.getIdAnnotationTargetRec(StockMethodsAdder.java:940)
Run Code Online (Sandbox Code Playgroud)
但首先,在声明之后
interface PropertyRepository : CrudRepository<Property, Pair<String, abc.model.Entity>>
Run Code Online (Sandbox Code Playgroud)
没有上述声明,就没有投诉,但没有可能主动管理Property.
如何规避这个错误?
我正在处理两个 JPA 实体:
1. 第一个命名Entity(不要误认为注释)
2. 第二个命名Property
AnEntity可以有 0..n 个Property. 代码如下:
@Entity
data class Entity (
@Id
@Column(name = "entity_id", updatable = false)
var entityId: String? = null,
@Column(nullable = true)
var type: …Run Code Online (Sandbox Code Playgroud) 我有一个ES6模块right.mjs。将其作为参数执行即可node:
$ node --version
v8.10.0
$ node --experimental-modules right.mjs
(node:4492) ExperimentalWarning: The ESM module loader is experimental.
executing right module
Run Code Online (Sandbox Code Playgroud)
executing right module 是模块的输出。
与此相反,REPL中的以下输入等待进一步的输入:
$ node --experimental-modules
> (node:4526) ExperimentalWarning: The ESM module loader is experimental.
> import 'right.mjs';
...
Run Code Online (Sandbox Code Playgroud)
我不明白为什么。
与:
> import './right.mjs';
...
Run Code Online (Sandbox Code Playgroud)
尝试require导致:
> require('./right.mjs');
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/xxx/right.mjs
at Object.Module._extensions..mjs (module.js:686:11)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require …Run Code Online (Sandbox Code Playgroud) 我的枚举泛型迭代器的(非编译)代码如下:
class EnumIterator<T: Enum<T>>: Iterator<T>
{
private var cursor = 0
override fun hasNext(): Boolean = cursor < enumValues<T>().size - 1
override fun next(): T = enumValues<T>()[cursor++]
}
Run Code Online (Sandbox Code Playgroud)
IntelliJ IDE将两个enumValues <T>中的T标记为红色:
“不能将'T'用作化类型参数。请改用类。”
如何声明类型参数以摆脱错误?
该文档页面的Reflect.apply()上MDN网页文档的状态:
在 ES5 中,您通常使用 Function.prototype.apply() 方法来调用具有给定 this 值和作为数组(或类似数组的对象)提供的参数的函数。
Function.prototype.apply.call(Math.floor, undefined, [1.75]);有了
Reflect.apply这个变得更简洁,更容易理解。
我糊涂了。在 ES5 中,我通常使用(保持上面的例子):
Math.floor.call(undefined, [1.75]);
为什么有人应该使用:
Function.prototype.apply.call(Math.floor, undefined, [1.75]);
PS:我的问题不是关于 Reflect.apply(myFunction, myObject, args);