From 2cb697f3dc72a88a74ef952b6ba8cdfb1214144d Mon Sep 17 00:00:00 2001 From: Hide_D Date: Tue, 17 May 2022 00:07:18 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20util/simpleSerialize=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/ts/util/simpleSerialize.ts | 17 +++++++++++++++++ src/sammo/Util.php | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 hwe/ts/util/simpleSerialize.ts diff --git a/hwe/ts/util/simpleSerialize.ts b/hwe/ts/util/simpleSerialize.ts new file mode 100644 index 00000000..7c4c7878 --- /dev/null +++ b/hwe/ts/util/simpleSerialize.ts @@ -0,0 +1,17 @@ +import { isInteger, isString } from "lodash"; + +export function simpleSerialize(...values : (string|number)[]): string{ + const result = []; + for(const value of values){ + if(isString(value)){ + result.push(`str(${value.length},${value})`); + continue; + } + if(isInteger(value)){ + result.push(`int(${value})`); + } + const float6 = value.toLocaleString("en-US", {maximumFractionDigits: 6}); + result.push(`float(${float6})`); + } + return result.join('|'); +} \ No newline at end of file diff --git a/src/sammo/Util.php b/src/sammo/Util.php index 7fb18f82..d18fefca 100644 --- a/src/sammo/Util.php +++ b/src/sammo/Util.php @@ -850,4 +850,25 @@ class Util extends \utilphp\util yield $a => $b; } } + + public static function simpleSerialize(string|int|float ...$values): string{ + $result = []; + foreach($values as $value){ + if(is_string($value)){ + $length = mb_strlen($value); + $result[] = "str({$length},{$value})"; + continue; + } + if(is_int($value)){ + $result[] = "int({$value})"; + continue; + } + if(is_float($value)){ + $value = number_format($value, 6, '.', ''); + $result[] = "float({$value})"; + continue; + } + } + return join('|', $result); + } };