diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2018-11-02 03:32:24 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2018-11-02 03:32:24 +0900 |
| commit | 931bdc6aace5e7aa71ffdfb470e208ead78a2a53 (patch) | |
| tree | eee6d7bf5f5480b883bb601517b4d9db03f31e9f /src/misc | |
| parent | Refactoring (diff) | |
| download | sharkey-931bdc6aace5e7aa71ffdfb470e208ead78a2a53.tar.gz sharkey-931bdc6aace5e7aa71ffdfb470e208ead78a2a53.tar.bz2 sharkey-931bdc6aace5e7aa71ffdfb470e208ead78a2a53.zip | |
Refactoring, Clean up and bug fixes
Diffstat (limited to 'src/misc')
| -rw-r--r-- | src/misc/cafy-id.ts | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/src/misc/cafy-id.ts b/src/misc/cafy-id.ts index 3880f0bd0c..621f7e7948 100644 --- a/src/misc/cafy-id.ts +++ b/src/misc/cafy-id.ts @@ -4,23 +4,31 @@ import isObjectId from './is-objectid'; export const isAnId = (x: any) => mongo.ObjectID.isValid(x); export const isNotAnId = (x: any) => !isAnId(x); +export const transform = (x: string | mongo.ObjectID): mongo.ObjectID => { + if (x == null) return null; + + if (isAnId(x) && !isObjectId(x)) { + return new mongo.ObjectID(x); + } else { + return x as mongo.ObjectID; + } +}; +export const transformMany = (xs: (string | mongo.ObjectID)[]): mongo.ObjectID[] => { + if (xs == null) return null; + + return xs.map(x => transform(x)); +}; + +export type ObjectId = mongo.ObjectID; /** * ID */ -export default class ID extends Context<mongo.ObjectID> { +export default class ID extends Context<string> { constructor() { super(); - this.transform = v => { - if (isAnId(v) && !isObjectId(v)) { - return new mongo.ObjectID(v); - } else { - return v; - } - }; - - this.push(v => { + this.push((v: any) => { if (!isObjectId(v) && isNotAnId(v)) { return new Error('must-be-an-id'); } |