@woosh/meep-engine 2.54.0 → 2.56.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/editor/view/node-graph/PortView.js +2 -2
- package/package.json +1 -1
- package/src/core/__module.js +1 -1
- package/src/core/binary/BinaryBuffer.js +2 -1
- package/src/core/cache/Cache.js +1 -1
- package/src/core/cache/LoadingCache.js +10 -5
- package/src/core/collection/Stack.js +11 -14
- package/src/core/collection/array/array_contains_duplicates.js +5 -2
- package/src/core/collection/array/array_contains_duplicates.spec.js +14 -0
- package/src/core/collection/array/array_copy_entire.js +6 -11
- package/src/core/collection/array/array_get_index_in_range.js +5 -0
- package/src/core/collection/array/array_get_index_in_range.spec.js +16 -0
- package/src/core/collection/array/array_remove_element.js +6 -9
- package/src/core/collection/array/array_remove_first.js +13 -6
- package/src/core/collection/array/array_remove_first.spec.js +39 -0
- package/src/core/collection/array/binarySearchHighIndex.spec.js +10 -9
- package/src/core/collection/array/groupArrayBy.js +1 -1
- package/src/core/collection/array/isArrayEqual.js +4 -1
- package/src/core/collection/array/randomizeArrayElementOrder.js +1 -0
- package/src/core/collection/array/randomizeArrayElementOrder.spec.js +27 -0
- package/src/core/collection/heap/Uin32Heap.spec.js +100 -0
- package/src/core/collection/heap/Uint32Heap.js +16 -7
- package/src/core/collection/list/List.js +1 -0
- package/src/core/collection/{HashMap.js → map/HashMap.js} +10 -10
- package/src/core/collection/{HashMap.spec.js → map/HashMap.spec.js} +1 -1
- package/src/core/collection/{ObservedMap.js → map/ObservedMap.js} +1 -1
- package/src/core/collection/{HashSet.js → set/HashSet.js} +4 -3
- package/src/core/collection/{Set.d.ts → set/Set.d.ts} +1 -1
- package/src/core/collection/{Set.js → set/Set.js} +17 -14
- package/src/core/function/FunctionCompiler.js +4 -4
- package/src/core/function/Functions.js +0 -19
- package/src/core/geom/2d/aabb/AABB2.js +1 -1
- package/src/core/geom/3d/SurfacePoint3.js +30 -20
- package/src/core/geom/3d/SurfacePoint3.spec.js +116 -0
- package/src/core/geom/3d/aabb/AABB3.js +13 -10
- package/src/core/geom/3d/aabb/{aabb3_array_contains_point.js → aabb3_array_intersects_point.js} +4 -1
- package/src/core/geom/3d/aabb/aabb3_from_min_max.js +25 -1
- package/src/core/geom/3d/aabb/aabb3_from_threejs_geometry.js +2 -25
- package/src/core/geom/3d/aabb/aabb3_signed_distance_sqr_to_point.js +5 -1
- package/src/core/geom/3d/apply_mat4_transform_to_direction_v3_array.js +5 -1
- package/src/core/geom/3d/apply_mat4_transform_to_v3_array.js +5 -1
- package/src/core/geom/3d/tetrahedra/TetrahedralMesh.js +2 -2
- package/src/core/geom/3d/topology/struct/binary/io/bt_mesh_calc_edges.js +1 -1
- package/src/core/geom/3d/triangle/computeTriangleRayIntersectionBarycentric.spec.js +26 -0
- package/src/core/geom/3d/v3_compute_triangle_normal.spec.js +18 -0
- package/src/core/geom/Vector2.js +1 -1
- package/src/core/geom/Vector3.js +1 -1
- package/src/core/geom/packing/miniball/Miniball.spec.js +24 -0
- package/src/core/math/statistics/computeStatisticalPartialMedian.js +2 -2
- package/src/core/model/ObservedInteger.js +12 -7
- package/src/core/model/ObservedString.js +10 -8
- package/src/core/model/node-graph/visual/NodeGraphVisualData.js +1 -1
- package/src/core/model/object/ImmutableObjectPool.js +1 -1
- package/src/core/model/object/read_property.js +2 -2
- package/src/core/model/object/write_property.js +3 -3
- package/src/core/model/reactive/evaluation/MultiPredicateEvaluator.js +1 -1
- package/src/core/primitives/numbers/compareNumbers.js +4 -4
- package/src/core/primitives/numbers/number_compare_ascending.js +9 -0
- package/src/core/primitives/numbers/number_compare_ascending.spec.js +9 -0
- package/src/core/primitives/numbers/number_compare_descending.js +9 -0
- package/src/core/primitives/numbers/number_compare_descending.spec.js +9 -0
- package/src/core/primitives/numbers/number_format_by_thousands.spec.js +12 -0
- package/src/core/primitives/numbers/number_pretty_print.js +1 -1
- package/src/core/primitives/strings/compareStrings.spec.js +12 -0
- package/src/core/primitives/strings/string_capitalize.js +15 -0
- package/src/core/primitives/strings/string_capitalize.spec.js +13 -0
- package/src/core/primitives/strings/string_compute_byte_size.js +21 -0
- package/src/core/primitives/strings/string_compute_common_prefix.js +44 -0
- package/src/core/primitives/strings/string_compute_common_prefix.spec.js +23 -0
- package/src/core/primitives/strings/string_format_camel_to_kebab.js +9 -0
- package/src/core/primitives/strings/string_format_camel_to_kebab.spec.js +8 -0
- package/src/core/primitives/strings/string_format_kebab_to_underscore.js +8 -0
- package/src/core/time/current_time_in_seconds.js +11 -0
- package/src/engine/Clock.js +3 -13
- package/src/engine/animation/curve/AnimationCurve.spec.js +27 -0
- package/src/engine/asset/AssetManager.js +5 -5
- package/src/engine/asset/loaders/GLTFAssetLoader.js +1 -1
- package/src/engine/ecs/EntityBlueprint.js +8 -10
- package/src/engine/ecs/EntityManager.js +8 -1
- package/src/engine/ecs/EntityManager.spec.js +56 -6
- package/src/engine/ecs/animation/Animation.spec.js +22 -0
- package/src/engine/ecs/animation/AnimationSerializationAdapter.js +2 -5
- package/src/engine/ecs/attachment/Attachment.js +24 -25
- package/src/engine/ecs/attachment/AttachmentBinding.js +27 -30
- package/src/engine/ecs/attachment/AttachmentSystem.js +21 -24
- package/src/engine/ecs/attachment/BoneAttachmentBinding.js +6 -9
- package/src/engine/ecs/attachment/TransformAttachmentBinding.js +0 -3
- package/src/engine/ecs/components/CharacterController.js +24 -18
- package/src/engine/ecs/components/Motion.js +0 -27
- package/src/engine/ecs/components/MotionSerializationAdapter.js +26 -0
- package/src/engine/ecs/components/PropertySet.js +0 -29
- package/src/engine/ecs/components/PropertySetSerializationAdapter.js +29 -0
- package/src/engine/ecs/components/SerializationMetadata.js +0 -28
- package/src/engine/ecs/components/SerializationMetadataSerializationAdapter.js +26 -0
- package/src/engine/ecs/components/Steering.js +0 -35
- package/src/engine/ecs/components/SteeringSerializationAdapter.js +34 -0
- package/src/engine/ecs/components/Tag.js +6 -102
- package/src/engine/ecs/components/TagSerializationAdapter.js +44 -0
- package/src/engine/ecs/components/TagSerializationUpgrader_0_1.js +18 -0
- package/src/engine/ecs/components/Timer.js +1 -1
- package/src/engine/ecs/dynamic_actions/DynamicActorSystem.js +4 -3
- package/src/engine/ecs/dynamic_actions/rules/DynamicRuleCooldownDescription.js +10 -14
- package/src/engine/ecs/dynamic_actions/rules/DynamicRuleDescription.js +31 -39
- package/src/engine/ecs/dynamic_actions/rules/DynamicRuleDescriptionTable.js +1 -1
- package/src/engine/ecs/dynamic_actions/{DynamicRuleDescriptionTable.spec.js → rules/DynamicRuleDescriptionTable.spec.js} +12 -12
- package/src/engine/ecs/foliage/ecs/InstancedMeshSerializationAdapter.js +2 -5
- package/src/engine/ecs/fow/FogOfWarRevealer.js +2 -3
- package/src/engine/ecs/fow/serialization/FogOfWarRevealerSerializationAdapter.js +2 -5
- package/src/engine/ecs/fow/serialization/FogOfWarSerializationAdapter.js +5 -8
- package/src/engine/ecs/gui/GUIElementSerializationAdapter.js +2 -5
- package/src/engine/ecs/gui/hud/HeadsUpDisplaySerializationAdapter.js +3 -6
- package/src/engine/ecs/gui/position/ViewportPosition.js +2 -5
- package/src/engine/ecs/ik/IKConstraint.js +95 -0
- package/src/engine/ecs/ik/IKMath.js +6 -1
- package/src/engine/ecs/ik/IKProblem.js +17 -17
- package/src/engine/ecs/ik/InverseKinematics.js +14 -174
- package/src/engine/ecs/ik/InverseKinematicsSerializationAdapter.js +72 -0
- package/src/engine/ecs/ik/InverseKinematicsSystem.js +24 -26
- package/src/engine/ecs/speaker/Voice.js +5 -8
- package/src/engine/ecs/speaker/VoiceSystem.js +15 -19
- package/src/engine/ecs/speaker/lines/LineDescription.js +20 -25
- package/src/engine/ecs/speaker/lines/sets/LineSetDescription.js +15 -19
- package/src/engine/ecs/storage/BinaryBufferSerializer.js +3 -3
- package/src/engine/ecs/storage/binary/BinaryClassSerializationAdapter.js +7 -0
- package/src/engine/ecs/storage/binary/collection/BinaryCollectionSerializer.js +1 -1
- package/src/engine/ecs/storage/binary/object/ObjectBasedClassSerializationAdapter.js +6 -9
- package/src/engine/ecs/systems/TagSystem.js +1 -6
- package/src/engine/ecs/team/TeamSerializationAdapter.js +2 -5
- package/src/engine/ecs/terrain/ecs/Terrain.spec.js +5 -0
- package/src/engine/ecs/terrain/ecs/cling/ClingToTerrain.js +23 -21
- package/src/engine/ecs/terrain/ecs/cling/ClingToTerrainSerializationAdapter.js +2 -5
- package/src/engine/ecs/terrain/ecs/layers/TerrainLayer.js +2 -2
- package/src/engine/ecs/terrain/ecs/layers/TerrainLayers.js +2 -40
- package/src/engine/ecs/terrain/serialization/TerrainSerializationAdapter.js +7 -10
- package/src/engine/ecs/tooltip/TooltipComponent.spec.js +47 -0
- package/src/engine/ecs/tooltip/TooltipComponentSerializationAdapter.js +2 -5
- package/src/engine/ecs/transform/Transform.spec.js +52 -0
- package/src/engine/ecs/transform/TransformSerializationAdapter.js +2 -5
- package/src/engine/ecs/transform-attachment/TransformAttachment.js +18 -19
- package/src/engine/graphics/ecs/animation/AnimationController.js +20 -129
- package/src/engine/graphics/ecs/animation/AnimationControllerSerializationAdapter.js +27 -0
- package/src/engine/graphics/ecs/animation/AnimationRule.js +79 -0
- package/src/engine/graphics/ecs/animation/animator/graph/AnimationGraphSerializationAdapter.js +9 -11
- package/src/engine/graphics/ecs/animation/animator/graph/definition/serialization/AnimationGraphDefinitionSerializationAdapter.js +3 -5
- package/src/engine/graphics/ecs/camera/serialization/CameraSerializationAdapter.js +2 -5
- package/src/engine/graphics/ecs/camera/topdown/TopDownCameraControllerSerializationAdapter.js +2 -6
- package/src/engine/graphics/ecs/camera/topdown/TopDownCameraLander.js +3 -15
- package/src/engine/graphics/ecs/decal/v2/DecalSerializationAdapter.js +2 -4
- package/src/engine/graphics/ecs/highlight/HighlightSerializationAdapter.js +2 -5
- package/src/engine/graphics/ecs/highlight/renderer/OutlineRenderer.js +1 -1
- package/src/engine/graphics/ecs/light/LightSerializationAdapter.js +2 -5
- package/src/engine/graphics/ecs/mesh/serialization/MeshSerializationAdapter.js +2 -5
- package/src/engine/graphics/ecs/mesh/skeleton/BoneMapping.js +2 -2
- package/src/engine/graphics/ecs/mesh-v2/ShadedGeometry.js +3 -0
- package/src/engine/graphics/ecs/mesh-v2/ShadedGeometry.spec.js +14 -0
- package/src/engine/graphics/ecs/mesh-v2/render/adapters/InstancedRendererAdapter.js +1 -1
- package/src/engine/graphics/ecs/mesh-v2/render/adapters/SGCacheKey.js +21 -9
- package/src/engine/graphics/ecs/mesh-v2/render/adapters/SGCacheKey.spec.js +79 -0
- package/src/engine/graphics/ecs/water/WaterSerializationAdapter.js +2 -5
- package/src/engine/graphics/geometry/optimization/merge/merge_geometry_hierarchy.js +1 -1
- package/src/engine/graphics/impostors/octahedral/ImpostorBaker.js +1 -1
- package/src/engine/graphics/material/manager/MaterialManager.js +1 -1
- package/src/engine/graphics/material/optimization/MaterialOptimizationContext.js +1 -1
- package/src/engine/graphics/particles/node-based/codegen/CodeContext.js +1 -1
- package/src/engine/graphics/particles/particular/engine/emitter/serde/ParameterLookupTableSerializationAdapter.js +2 -5
- package/src/engine/graphics/particles/particular/engine/emitter/serde/ParticleEmitterSerializationAdapter.js +2 -5
- package/src/engine/graphics/particles/particular/engine/shader/ShaderManager.js +1 -1
- package/src/engine/graphics/render/visibility/IncrementalDeltaSet.spec.js +7 -6
- package/src/engine/graphics/texture/atlas/ManagedTextureAtlas.js +1 -1
- package/src/engine/graphics/texture/sampler/serialization/Sampler2DSerializationAdapter.js +2 -5
- package/src/engine/grid/grid2transform/GridPosition2Transform.js +11 -13
- package/src/engine/grid/grid2transform/GridPosition2TransformSerializationAdapter.js +3 -5
- package/src/engine/grid/obstacle/GridObstacleSerializationAdapter.js +2 -5
- package/src/engine/grid/position/GridPositionSerializationAdapter.js +2 -5
- package/src/engine/grid/transform2grid/Transform2GridPosition.js +10 -13
- package/src/engine/input/devices/KeyboardDevice.js +5 -2
- package/src/engine/input/devices/KeyboardDevice.spec.js +12 -0
- package/src/engine/input/devices/PointerDevice.js +5 -1
- package/src/engine/input/ecs/components/Input.js +37 -11
- package/src/engine/input/ecs/ism/InputBinding.js +30 -12
- package/src/engine/intelligence/behavior/ecs/BehaviorComponentSerializationAdapter.js +7 -10
- package/src/engine/intelligence/behavior/ecs/WaitForEventBehaviorSerializationAdapter.js +2 -5
- package/src/engine/intelligence/behavior/primitive/FailingBehaviorSerializationAdapter.js +2 -6
- package/src/engine/intelligence/behavior/primitive/SucceedingBehaviorSerializationAdapter.js +2 -5
- package/src/engine/intelligence/blackboard/BlackboardSerializationAdapter.js +2 -5
- package/src/engine/knowledge/database/StaticKnowledgeDatabase.js +12 -13
- package/src/engine/knowledge/database/StaticKnowledgeDatabase.spec.js +23 -0
- package/src/engine/navigation/ecs/components/PathSerializationAdapter.js +2 -5
- package/src/engine/navigation/ecs/path_following/PathFollowerSerializationAdapter.js +2 -5
- package/src/engine/navigation/grid/find_path_on_grid_astar.spec.js +25 -0
- package/src/engine/options/OptionAbstract.js +6 -5
- package/src/engine/options/OptionGroup.js +13 -25
- package/src/engine/options/OptionGroup.spec.js +61 -0
- package/src/engine/save/storage/InMemoryStorage.js +3 -2
- package/src/engine/simulation/Ticker.js +17 -20
- package/src/engine/sound/ecs/SoundController.js +2 -6
- package/src/engine/sound/ecs/SoundListener.js +3 -21
- package/src/engine/sound/ecs/emitter/SoundEmitterSerializationAdapter.js +2 -6
- package/src/generation/GridTaskGroup.js +5 -9
- package/src/generation/filtering/numeric/CellFilterCache.js +12 -16
- package/src/generation/filtering/numeric/complex/CellFilterFXAA.js +31 -32
- package/src/generation/filtering/numeric/complex/CellFilterLookupTable.js +6 -9
- package/src/generation/filtering/numeric/complex/CellFilterSimplexNoise.js +7 -10
- package/src/generation/filtering/numeric/complex/CellFilterSobel.js +6 -9
- package/src/generation/filtering/numeric/math/CellFilterMembershipGeneralizedBell.js +18 -21
- package/src/generation/filtering/numeric/sampling/AbstractCellFilterSampleGridLayer.js +24 -25
- package/src/generation/filtering/numeric/sampling/CellFilterSampleLayerLinear.js +1 -1
- package/src/generation/grid/GridData.js +23 -22
- package/src/generation/grid/GridData.spec.js +41 -0
- package/src/generation/grid/GridTaskGenerator.js +7 -8
- package/src/generation/grid/layers/GridDataLayer.js +23 -25
- package/src/generation/markers/GridActionRuleSet.js +20 -22
- package/src/generation/markers/GridCellActionPlaceMarker.js +40 -43
- package/src/generation/markers/GridCellActionPlaceMarkerGroup.js +7 -9
- package/src/generation/markers/MarkerNode.js +44 -44
- package/src/generation/markers/actions/MarkerNodeActionEntityPlacement.js +15 -18
- package/src/generation/markers/actions/MarkerNodeActionSequence.js +6 -9
- package/src/generation/markers/actions/MarkerNodeProcessingRuleSet.js +5 -7
- package/src/generation/markers/actions/MarkerProcessingRule.js +25 -26
- package/src/generation/markers/actions/placement/MarkerNodeEntityProcessorClingToTerrain.js +12 -15
- package/src/generation/markers/actions/placement/MarkerNodeEntityProcessorRandomRotation.js +2 -5
- package/src/generation/markers/actions/placement/MarkerNodeEntityProcessorSequence.js +6 -9
- package/src/generation/markers/actions/probability/MarkerNodeActionSelectWeighted.js +8 -11
- package/src/generation/markers/actions/probability/MarkerNodeActionWeightedElement.js +13 -13
- package/src/generation/markers/actions/util/GridCellActionDebugBreak.js +5 -8
- package/src/generation/markers/actions/util/GridCellActionLogToConsole.js +1 -4
- package/src/generation/markers/emitter/MarkerNodeConsumerBuffer.js +10 -13
- package/src/generation/markers/emitter/MarkerNodeEmitterFromAction.js +5 -9
- package/src/generation/markers/emitter/MarkerNodeEmitterGridCellAction.js +12 -15
- package/src/generation/markers/emitter/MarkerNodeEmitterGroup.js +5 -8
- package/src/generation/markers/emitter/MarkerNodeEmitterPredicated.js +18 -21
- package/src/generation/markers/matcher/MarkerNodeMatcher.js +2 -1
- package/src/generation/markers/matcher/MarkerNodeMatcherBinary.js +12 -13
- package/src/generation/markers/matcher/MarkerNodeMatcherContainsTag.js +7 -9
- package/src/generation/markers/matcher/MarkerNodeMatcherNot.js +7 -9
- package/src/generation/markers/predicate/GridDataNodePredicateBinary.js +10 -14
- package/src/generation/markers/predicate/GridDataNodePredicateNot.js +9 -11
- package/src/generation/markers/predicate/GridDataNodePredicateOverlaps.js +6 -9
- package/src/generation/markers/transform/MarkerNodeTransformRotateRandom.js +2 -6
- package/src/generation/markers/transform/MarkerNodeTransformerAddPositionYFromFilter.js +6 -9
- package/src/generation/markers/transform/MarkerNodeTransformerOffsetPosition.js +1 -4
- package/src/generation/markers/transform/MarkerNodeTransformerRecordProperty.js +12 -15
- package/src/generation/markers/transform/MarkerNodeTransformerRecordPropertyClosure.js +14 -17
- package/src/generation/markers/transform/MarkerNodeTransformerRecordUniqueRandomEnum.js +21 -23
- package/src/generation/markers/transform/MarkerNodeTransformerRemoveTag.js +1 -4
- package/src/generation/markers/transform/MarkerNodeTransformerSequence.js +6 -9
- package/src/generation/markers/transform/MarkerNodeTransformerYRotateByFilter.js +13 -17
- package/src/generation/markers/transform/MarkerNodeTransformerYRotateByFilterGradient.js +12 -16
- package/src/generation/placement/GridCellActionTransformNearbyMarkers.js +26 -29
- package/src/generation/placement/GridCellPlacementRule.js +30 -32
- package/src/generation/placement/action/GridCellActionPlaceTags.js +26 -28
- package/src/generation/placement/action/GridCellActionWriteFilterToLayer.js +16 -20
- package/src/generation/placement/action/random/weighted/CellActionSelectWeightedRandom.js +13 -16
- package/src/generation/placement/action/random/weighted/WeightedGridCellAction.js +11 -14
- package/src/generation/placement/action/util/CellMatcherWithinAABB.js +2 -6
- package/src/generation/placement/action/util/GridCellActionSequence.js +7 -9
- package/src/generation/placement/action/util/GridCellDisplacedAction.js +9 -12
- package/src/generation/rules/CellMatcherFromFilter.js +6 -9
- package/src/generation/rules/CellMatcherLayerBitMaskTest.js +6 -9
- package/src/generation/rules/GridLayerCellMatcher.js +11 -14
- package/src/generation/rules/cell/CellMatcherContainsMarkerWithinRadius.js +11 -14
- package/src/generation/rules/cell/CellMatcherGridPattern.js +6 -8
- package/src/generation/rules/cell/GridPatternMatcherCell.js +11 -12
- package/src/generation/rules/logic/CellMatcherBinary.js +10 -14
- package/src/generation/rules/logic/CellMatcherDecorator.js +5 -8
- package/src/generation/theme/AreaMask.js +15 -17
- package/src/generation/theme/AreaTheme.js +7 -8
- package/src/generation/theme/TerrainLayerDescription.js +10 -12
- package/src/generation/theme/TerrainLayerRule.js +11 -13
- package/src/generation/theme/TerrainTheme.js +6 -7
- package/src/generation/theme/Theme.js +15 -17
- package/src/generation/theme/ThemeEngine.js +17 -19
- package/src/view/string_tag_to_css_class_name.js +2 -2
- package/src/view/tooltip/gml/TooltipParser.js +2 -2
- package/src/core/cache/PersistentCacheAdapter.js +0 -378
- package/src/core/collection/LazyStream.js +0 -23
- package/src/core/collection/LazyStream.spec.js +0 -13
- package/src/core/collection/heap/BinaryHeap.js +0 -153
- package/src/core/primitives/strings/StringUtils.js +0 -105
- package/src/core/primitives/strings/StringUtils.spec.js +0 -42
- package/src/engine/ecs/components/GeometryBVH.js +0 -44
- package/src/engine/ecs/components/MonsterAI.js +0 -15
- package/src/engine/ecs/systems/GeometryBVHSystem.js +0 -36
- package/src/generation/markers/MarkerRelation.js +0 -13
- /package/src/core/collection/{HashMap.d.ts → map/HashMap.d.ts} +0 -0
- /package/src/core/collection/{HashSet.d.ts → set/HashSet.d.ts} +0 -0
|
@@ -1,378 +0,0 @@
|
|
|
1
|
-
import { IndexedDBStorage } from "../../engine/save/storage/IndexedDBStorage.js";
|
|
2
|
-
import { assert } from "../assert.js";
|
|
3
|
-
import { Base64 } from "../binary/Base64.js";
|
|
4
|
-
import { BinaryBuffer } from "../binary/BinaryBuffer.js";
|
|
5
|
-
|
|
6
|
-
const scratch_buffer = new BinaryBuffer();
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
*
|
|
10
|
-
* @type {number}
|
|
11
|
-
*/
|
|
12
|
-
const BUFFER_MAX_RETAINED_SIZE = 131072;
|
|
13
|
-
|
|
14
|
-
function cleanup_buffer() {
|
|
15
|
-
if (scratch_buffer.length > BUFFER_MAX_RETAINED_SIZE) {
|
|
16
|
-
scratch_buffer.position = 0;
|
|
17
|
-
scratch_buffer.setCapacity(BUFFER_MAX_RETAINED_SIZE);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @template T
|
|
23
|
-
* @param {T} object
|
|
24
|
-
* @param {BinaryClassSerializationAdapter} adapter
|
|
25
|
-
* @returns {string}
|
|
26
|
-
*/
|
|
27
|
-
function object2string(object, adapter) {
|
|
28
|
-
|
|
29
|
-
scratch_buffer.position = 0;
|
|
30
|
-
|
|
31
|
-
// serialize key
|
|
32
|
-
adapter.serialize(scratch_buffer, object);
|
|
33
|
-
|
|
34
|
-
// convert key to string
|
|
35
|
-
const end = scratch_buffer.position;
|
|
36
|
-
const array_buffer = scratch_buffer.data.slice(0, end);
|
|
37
|
-
|
|
38
|
-
const result = Base64.encode(array_buffer);
|
|
39
|
-
|
|
40
|
-
cleanup_buffer();
|
|
41
|
-
|
|
42
|
-
return result;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* @template T
|
|
47
|
-
* @param {string} string
|
|
48
|
-
* @param {BinaryClassSerializationAdapter} adapter
|
|
49
|
-
* @param {Class<T>} klass
|
|
50
|
-
* @returns T
|
|
51
|
-
*/
|
|
52
|
-
function string2object(string, adapter, klass) {
|
|
53
|
-
/**
|
|
54
|
-
*
|
|
55
|
-
* @type {T}
|
|
56
|
-
*/
|
|
57
|
-
const result = new klass();
|
|
58
|
-
|
|
59
|
-
const arrayBuffer = Base64.decode(string);
|
|
60
|
-
|
|
61
|
-
scratch_buffer.fromArrayBuffer(arrayBuffer);
|
|
62
|
-
|
|
63
|
-
adapter.deserialize(scratch_buffer, result);
|
|
64
|
-
|
|
65
|
-
cleanup_buffer();
|
|
66
|
-
|
|
67
|
-
return result;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Attaches to a cache instance and persists all the cache data asynchronously
|
|
72
|
-
* Behaves similarly to WeakMap class with similar interface.
|
|
73
|
-
* @template K,V
|
|
74
|
-
*/
|
|
75
|
-
export class PersistentCacheAdapter {
|
|
76
|
-
/**
|
|
77
|
-
* @template K,V
|
|
78
|
-
* @param {string} storageKey
|
|
79
|
-
* @param {BinaryClassSerializationAdapter} keyAdapter
|
|
80
|
-
* @param {Class<K>|function} keyClass
|
|
81
|
-
* @param {BinaryClassSerializationAdapter} valueAdapter
|
|
82
|
-
* @param {Class<V>|function} valueClass
|
|
83
|
-
*/
|
|
84
|
-
constructor({
|
|
85
|
-
storageKey,
|
|
86
|
-
keyAdapter,
|
|
87
|
-
keyClass,
|
|
88
|
-
valueAdapter,
|
|
89
|
-
valueClass
|
|
90
|
-
}) {
|
|
91
|
-
assert.typeOf(storageKey, 'string', 'storageKey');
|
|
92
|
-
|
|
93
|
-
assert.defined(keyAdapter, 'keyAdapter');
|
|
94
|
-
assert.notNull(keyAdapter, 'keyAdapter');
|
|
95
|
-
assert.defined(keyClass, 'keyClass');
|
|
96
|
-
assert.notNull(keyClass, 'keyClass');
|
|
97
|
-
|
|
98
|
-
assert.defined(valueAdapter, 'valueAdapter');
|
|
99
|
-
assert.notNull(valueAdapter, 'valueAdapter');
|
|
100
|
-
assert.defined(valueClass, 'valueClass');
|
|
101
|
-
assert.notNull(valueClass, 'valueClass');
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
*
|
|
105
|
-
* @type {string}
|
|
106
|
-
* @private
|
|
107
|
-
*/
|
|
108
|
-
this.__storage_key = storageKey;
|
|
109
|
-
/**
|
|
110
|
-
*
|
|
111
|
-
* @type {BinaryClassSerializationAdapter}
|
|
112
|
-
* @private
|
|
113
|
-
*/
|
|
114
|
-
this.__key_adapter = keyAdapter;
|
|
115
|
-
/**
|
|
116
|
-
*
|
|
117
|
-
* @type {Class<K>|Function}
|
|
118
|
-
* @private
|
|
119
|
-
*/
|
|
120
|
-
this.__key_class = keyClass;
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
*
|
|
124
|
-
* @type {BinaryClassSerializationAdapter}
|
|
125
|
-
* @private
|
|
126
|
-
*/
|
|
127
|
-
this.__value_adapter = valueAdapter;
|
|
128
|
-
/**
|
|
129
|
-
*
|
|
130
|
-
* @type {Class<V>|Function}
|
|
131
|
-
* @private
|
|
132
|
-
*/
|
|
133
|
-
this.__value_class = valueClass;
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
*
|
|
137
|
-
* @type {Storage}
|
|
138
|
-
* @private
|
|
139
|
-
*/
|
|
140
|
-
this.__storage = new IndexedDBStorage(`meep.core.persistent_cache/${storageKey}`);
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
*
|
|
144
|
-
* @type {Cache|null}
|
|
145
|
-
* @private
|
|
146
|
-
*/
|
|
147
|
-
this.__cache = null;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
*
|
|
152
|
-
* @param {Cache<K,V>} cache
|
|
153
|
-
* @param {boolean} ingest should data stored in the persisted cache be uploaded to the cache or not
|
|
154
|
-
*/
|
|
155
|
-
async attach(cache, ingest = true) {
|
|
156
|
-
if (this.__cache !== null) {
|
|
157
|
-
throw new Error(`Adapted already connected to a cache`);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
this.__cache = cache;
|
|
161
|
-
|
|
162
|
-
// connect signals
|
|
163
|
-
cache.onSet.add(this.__handle_set, this);
|
|
164
|
-
cache.onRemoved.add(this.__handle_remove, this);
|
|
165
|
-
|
|
166
|
-
if (ingest) {
|
|
167
|
-
await this.__ingest();
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
*
|
|
173
|
-
* @return {Promise<void>}
|
|
174
|
-
* @private
|
|
175
|
-
*/
|
|
176
|
-
async __ingest() {
|
|
177
|
-
/**
|
|
178
|
-
*
|
|
179
|
-
* @type {string[]}
|
|
180
|
-
*/
|
|
181
|
-
const keys = await this.__storage.promiseList();
|
|
182
|
-
|
|
183
|
-
const key_count = keys.length;
|
|
184
|
-
|
|
185
|
-
const pairs = [];
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
*
|
|
189
|
-
* @type {Promise[]}
|
|
190
|
-
*/
|
|
191
|
-
const read_promises = [];
|
|
192
|
-
|
|
193
|
-
const start = performance.now();
|
|
194
|
-
|
|
195
|
-
for (let i = 0; i < key_count; i++) {
|
|
196
|
-
const key_string = keys[i];
|
|
197
|
-
|
|
198
|
-
const promise = new Promise(async (resolve, reject) => {
|
|
199
|
-
const key = string2object(key_string, this.__key_adapter, this.__key_class);
|
|
200
|
-
|
|
201
|
-
// read value
|
|
202
|
-
let value;
|
|
203
|
-
|
|
204
|
-
try {
|
|
205
|
-
value = await this.__get_by_string_unsafe(key_string);
|
|
206
|
-
} catch (e) {
|
|
207
|
-
|
|
208
|
-
// failed to read value, skip
|
|
209
|
-
resolve();
|
|
210
|
-
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// first read into temp array to prevent thrashing when adding one thing to cache would cause another thing to be removed
|
|
215
|
-
pairs.push([key, value]);
|
|
216
|
-
|
|
217
|
-
resolve();
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
read_promises.push(promise);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
await Promise.all(read_promises);
|
|
224
|
-
|
|
225
|
-
console.log(`${this.__storage_key} : Ingesting ${key_count} values finished after ${performance.now() - start}ms`);
|
|
226
|
-
|
|
227
|
-
const cache = this.__cache;
|
|
228
|
-
|
|
229
|
-
// disconnect signals
|
|
230
|
-
cache.onSet.remove(this.__handle_set, this);
|
|
231
|
-
cache.onRemoved.remove(this.__handle_remove, this);
|
|
232
|
-
|
|
233
|
-
// add to cache
|
|
234
|
-
for (let i = 0; i < pairs.length; i++) {
|
|
235
|
-
const [key, value] = pairs[i];
|
|
236
|
-
|
|
237
|
-
cache.put(key, value);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// reconnect signals
|
|
241
|
-
cache.onSet.add(this.__handle_set, this);
|
|
242
|
-
cache.onRemoved.add(this.__handle_remove, this);
|
|
243
|
-
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
*
|
|
248
|
-
* @param {K} key
|
|
249
|
-
* @param {V} value
|
|
250
|
-
* @private
|
|
251
|
-
*/
|
|
252
|
-
__handle_set(key, value) {
|
|
253
|
-
this.set(key, value);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
*
|
|
258
|
-
* @param {K} key
|
|
259
|
-
* @private
|
|
260
|
-
*/
|
|
261
|
-
__handle_remove(key) {
|
|
262
|
-
this.delete(key);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
*
|
|
268
|
-
* @param {K} key
|
|
269
|
-
* @param {V} value
|
|
270
|
-
* @private
|
|
271
|
-
*/
|
|
272
|
-
async set(key, value) {
|
|
273
|
-
assert.defined(key, 'key');
|
|
274
|
-
assert.notNull(key, 'key');
|
|
275
|
-
|
|
276
|
-
assert.defined(value, 'value');
|
|
277
|
-
assert.notNull(value, 'value');
|
|
278
|
-
|
|
279
|
-
const key_string = object2string(key, this.__key_adapter);
|
|
280
|
-
|
|
281
|
-
// serialize value
|
|
282
|
-
scratch_buffer.position = 0;
|
|
283
|
-
this.__value_adapter.serialize(scratch_buffer, value);
|
|
284
|
-
|
|
285
|
-
cleanup_buffer();
|
|
286
|
-
|
|
287
|
-
const value_data = scratch_buffer.data.slice(0, scratch_buffer.position);
|
|
288
|
-
|
|
289
|
-
await this.__storage.promiseStoreBinary(key_string, value_data);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
*
|
|
294
|
-
* @param {K} key
|
|
295
|
-
* @returns {V|undefined}
|
|
296
|
-
* @private
|
|
297
|
-
*/
|
|
298
|
-
async get(key) {
|
|
299
|
-
assert.defined(key, 'key');
|
|
300
|
-
assert.notNull(key, 'key');
|
|
301
|
-
|
|
302
|
-
const key_string = object2string(key, this.__key_adapter);
|
|
303
|
-
|
|
304
|
-
// check if value is present
|
|
305
|
-
const exists = await this.__storage.promiseContains(key_string);
|
|
306
|
-
|
|
307
|
-
if (!exists) {
|
|
308
|
-
// value doesn't exist
|
|
309
|
-
return undefined;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
return this.__get_by_string_unsafe(key_string);
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
* @param {string} key_string
|
|
317
|
-
* @return {Promise<V>}
|
|
318
|
-
* @private
|
|
319
|
-
*/
|
|
320
|
-
async __get_by_string_unsafe(key_string) {
|
|
321
|
-
|
|
322
|
-
/**
|
|
323
|
-
*
|
|
324
|
-
* @type {ArrayBuffer}
|
|
325
|
-
*/
|
|
326
|
-
const value_data = await this.__storage.promiseLoadBinary(key_string);
|
|
327
|
-
|
|
328
|
-
const value = new this.__value_class();
|
|
329
|
-
|
|
330
|
-
scratch_buffer.fromArrayBuffer(value_data);
|
|
331
|
-
|
|
332
|
-
try {
|
|
333
|
-
this.__value_adapter.deserialize(scratch_buffer, value);
|
|
334
|
-
} catch (e) {
|
|
335
|
-
// failed to deserialize
|
|
336
|
-
// console.error('Failed to deserialize the value: ', e);
|
|
337
|
-
|
|
338
|
-
// delete the value
|
|
339
|
-
this.__storage.promiseRemove(key_string);
|
|
340
|
-
|
|
341
|
-
throw new Error('Failed to deserialize');
|
|
342
|
-
} finally {
|
|
343
|
-
cleanup_buffer();
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
return value;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
*
|
|
351
|
-
* @param {K} key
|
|
352
|
-
* @returns {boolean}
|
|
353
|
-
* @private
|
|
354
|
-
*/
|
|
355
|
-
async has(key) {
|
|
356
|
-
assert.defined(key, 'key');
|
|
357
|
-
assert.notNull(key, 'key');
|
|
358
|
-
|
|
359
|
-
const key_string = object2string(key, this.__key_adapter);
|
|
360
|
-
|
|
361
|
-
return await this.__storage.promiseContains(key_string);
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
/**
|
|
365
|
-
*
|
|
366
|
-
* @param {K} key
|
|
367
|
-
* @private
|
|
368
|
-
*/
|
|
369
|
-
async delete(key) {
|
|
370
|
-
assert.defined(key, 'key');
|
|
371
|
-
assert.notNull(key, 'key');
|
|
372
|
-
|
|
373
|
-
const key_string = object2string(key, this.__key_adapter);
|
|
374
|
-
|
|
375
|
-
return await this.__storage.promiseRemove(key_string);
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @template A,B
|
|
3
|
-
* @param {A[]} source
|
|
4
|
-
* @param {function(A,index:number):B} transformer
|
|
5
|
-
* @returns {B[]}
|
|
6
|
-
*/
|
|
7
|
-
export function lazyArrayMap(source, transformer) {
|
|
8
|
-
return new Proxy(source, {
|
|
9
|
-
get(target, p, receiver) {
|
|
10
|
-
if (/^([0-9]+)$/.test(p)) {
|
|
11
|
-
|
|
12
|
-
const targetElement = target[p];
|
|
13
|
-
|
|
14
|
-
const resultElement = transformer(targetElement, p);
|
|
15
|
-
|
|
16
|
-
return resultElement;
|
|
17
|
-
} else {
|
|
18
|
-
|
|
19
|
-
return target[p];
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { lazyArrayMap } from "./LazyStream.js";
|
|
2
|
-
|
|
3
|
-
test('lazyArrayMap', () => {
|
|
4
|
-
const source = [1, 2, 3];
|
|
5
|
-
|
|
6
|
-
const map = lazyArrayMap(source, a => a * 2);
|
|
7
|
-
|
|
8
|
-
expect(map.length).toBe(source.length);
|
|
9
|
-
|
|
10
|
-
for (let i = 0; i < source.length; i++) {
|
|
11
|
-
expect(map[i]).toBe(source[i] * 2);
|
|
12
|
-
}
|
|
13
|
-
});
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @deprecated use {@link FastBinaryHeap} instead
|
|
3
|
-
* @param scoreFunction
|
|
4
|
-
* @constructor
|
|
5
|
-
*/
|
|
6
|
-
function BinaryHeap(scoreFunction) {
|
|
7
|
-
this.content = [];
|
|
8
|
-
this.hash = [];
|
|
9
|
-
this.scoreFunction = scoreFunction;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
BinaryHeap.prototype = {
|
|
13
|
-
push: function (element) {
|
|
14
|
-
const content = this.content;
|
|
15
|
-
const length = content.length;
|
|
16
|
-
// Add the new element to the end of the array.
|
|
17
|
-
content.push(element);
|
|
18
|
-
//update hash
|
|
19
|
-
this.hash[element] = length;
|
|
20
|
-
|
|
21
|
-
// Allow it to sink down.
|
|
22
|
-
this.sinkDown(length);
|
|
23
|
-
},
|
|
24
|
-
pop: function () {
|
|
25
|
-
// Store the first element so we can return it later.
|
|
26
|
-
const content = this.content;
|
|
27
|
-
const hash = this.hash;
|
|
28
|
-
const result = content[0];
|
|
29
|
-
//update hash
|
|
30
|
-
delete hash[result];
|
|
31
|
-
// Get the element at the end of the array.
|
|
32
|
-
const end = content.pop();
|
|
33
|
-
// If there are any elements left, put the end element at the
|
|
34
|
-
// start, and let it bubble up.
|
|
35
|
-
const length = content.length;
|
|
36
|
-
if (length > 0) {
|
|
37
|
-
content[0] = end;
|
|
38
|
-
hash[end] = 0;
|
|
39
|
-
this.bubbleUp(0);
|
|
40
|
-
}
|
|
41
|
-
return result;
|
|
42
|
-
},
|
|
43
|
-
contains: function (node) {
|
|
44
|
-
return this.hash[node] !== void 0;
|
|
45
|
-
},
|
|
46
|
-
remove: function (node) {
|
|
47
|
-
|
|
48
|
-
const length = this.content.length;
|
|
49
|
-
const i = this.hash[node];
|
|
50
|
-
|
|
51
|
-
// When it is found, the process seen in 'pop' is repeated
|
|
52
|
-
// to fill up the hole.
|
|
53
|
-
const end = this.content.pop();
|
|
54
|
-
if (i !== length) {
|
|
55
|
-
this.content[i] = end;
|
|
56
|
-
|
|
57
|
-
if (this.scoreFunction(end) < this.scoreFunction(node)) {
|
|
58
|
-
this.sinkDown(i);
|
|
59
|
-
} else {
|
|
60
|
-
this.bubbleUp(i);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
size: function () {
|
|
65
|
-
return this.content.length;
|
|
66
|
-
},
|
|
67
|
-
rescoreElement: function (node) {
|
|
68
|
-
this.sinkDown(this.hash[node]);
|
|
69
|
-
},
|
|
70
|
-
sinkDown: function (n) {
|
|
71
|
-
// Fetch the element that has to be sunk.
|
|
72
|
-
const content = this.content;
|
|
73
|
-
const hash = this.hash;
|
|
74
|
-
const element = content[n];
|
|
75
|
-
|
|
76
|
-
// When at 0, an element can not sink any further.
|
|
77
|
-
while (n > 0) {
|
|
78
|
-
|
|
79
|
-
// Compute the parent element's index, and fetch it.
|
|
80
|
-
const parentN = ((n + 1) >> 1) - 1,
|
|
81
|
-
parent = content[parentN];
|
|
82
|
-
// Swap the elements if the parent is greater.
|
|
83
|
-
if (this.scoreFunction(element) < this.scoreFunction(parent)) {
|
|
84
|
-
content[parentN] = element;
|
|
85
|
-
content[n] = parent;
|
|
86
|
-
//update hash
|
|
87
|
-
hash[element] = parentN;
|
|
88
|
-
hash[parent] = n;
|
|
89
|
-
// Update 'n' to continue at the new position.
|
|
90
|
-
n = parentN;
|
|
91
|
-
}
|
|
92
|
-
// Found a parent that is less, no need to sink any further.
|
|
93
|
-
else {
|
|
94
|
-
break;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
},
|
|
98
|
-
bubbleUp: function (n) {
|
|
99
|
-
// Look up the target element and its score.
|
|
100
|
-
const content = this.content;
|
|
101
|
-
const hash = this.hash;
|
|
102
|
-
const scoreFunction = this.scoreFunction;
|
|
103
|
-
const length = content.length,
|
|
104
|
-
element = content[n],
|
|
105
|
-
elemScore = scoreFunction(element);
|
|
106
|
-
|
|
107
|
-
for (; ;) {
|
|
108
|
-
// Compute the indices of the child elements.
|
|
109
|
-
const child2N = (n + 1) << 1,
|
|
110
|
-
child1N = child2N - 1;
|
|
111
|
-
// This is used to store the new position of the element, if any.
|
|
112
|
-
let swap = null,
|
|
113
|
-
child1Score;
|
|
114
|
-
// If the first child exists (is inside the array)...
|
|
115
|
-
if (child1N < length) {
|
|
116
|
-
// Look it up and compute its score.
|
|
117
|
-
const child1 = content[child1N];
|
|
118
|
-
child1Score = scoreFunction(child1);
|
|
119
|
-
|
|
120
|
-
// If the score is less than our element's, we need to swap.
|
|
121
|
-
if (child1Score < elemScore) {
|
|
122
|
-
swap = child1N;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// Do the same checks for the other child.
|
|
128
|
-
if (child2N < length) {
|
|
129
|
-
const child2 = content[child2N],
|
|
130
|
-
child2Score = scoreFunction(child2);
|
|
131
|
-
if (child2Score < (swap === null ? elemScore : child1Score)) {
|
|
132
|
-
swap = child2N;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// If the element needs to be moved, swap it, and continue.
|
|
137
|
-
if (swap !== null) {
|
|
138
|
-
const swapContent = content[swap];
|
|
139
|
-
content[n] = swapContent;
|
|
140
|
-
content[swap] = element;
|
|
141
|
-
//update hash
|
|
142
|
-
hash[element] = swap;
|
|
143
|
-
hash[swapContent] = n;
|
|
144
|
-
n = swap;
|
|
145
|
-
}
|
|
146
|
-
// Otherwise, we are done.
|
|
147
|
-
else {
|
|
148
|
-
break;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
export default BinaryHeap;
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Alex Goldring 2018
|
|
3
|
-
* @copyright Alex Goldring 2018
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Compute byte size of a UTF8 string
|
|
8
|
-
* @param {string} value
|
|
9
|
-
* @returns {number}
|
|
10
|
-
*/
|
|
11
|
-
export function computeUTF8StringByteSize(value) {
|
|
12
|
-
const length = value.length;
|
|
13
|
-
|
|
14
|
-
let p = 0;
|
|
15
|
-
|
|
16
|
-
for (let i = 0; i < length; i++) {
|
|
17
|
-
let c = value.charCodeAt(i);
|
|
18
|
-
while (c > 0xff) {
|
|
19
|
-
p++;
|
|
20
|
-
c >>= 8;
|
|
21
|
-
}
|
|
22
|
-
p++;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return p;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
*
|
|
30
|
-
* @param {string} string
|
|
31
|
-
* @returns {string}
|
|
32
|
-
*/
|
|
33
|
-
export function capitalize(string) {
|
|
34
|
-
const length = string.length;
|
|
35
|
-
|
|
36
|
-
if (length === 0) {
|
|
37
|
-
return string;
|
|
38
|
-
} else {
|
|
39
|
-
return string.charAt(0).toLocaleUpperCase() + string.substring(1);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* @example: ['abra', 'abc', 'abode'] => 'ab'
|
|
45
|
-
* @param {String[]} strings
|
|
46
|
-
* @returns string
|
|
47
|
-
*/
|
|
48
|
-
export function computeCommonPrefix(strings) {
|
|
49
|
-
let i, j;
|
|
50
|
-
|
|
51
|
-
const numInputs = strings.length;
|
|
52
|
-
|
|
53
|
-
let result = "";
|
|
54
|
-
|
|
55
|
-
if (numInputs === 0) {
|
|
56
|
-
return result;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const firstString = strings[0];
|
|
60
|
-
|
|
61
|
-
let lengthLimit = firstString.length;
|
|
62
|
-
|
|
63
|
-
for (i = 1; i < numInputs; i++) {
|
|
64
|
-
lengthLimit = Math.min(strings[i].length, lengthLimit);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
main_loop:for (i = 0; i < lengthLimit; i++) {
|
|
69
|
-
const letter0 = firstString.charAt(i);
|
|
70
|
-
|
|
71
|
-
for (j = 1; j < numInputs; j++) {
|
|
72
|
-
|
|
73
|
-
const string = strings[j];
|
|
74
|
-
|
|
75
|
-
const letter1 = string.charAt(i);
|
|
76
|
-
|
|
77
|
-
if (letter0 !== letter1) {
|
|
78
|
-
break main_loop;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
result += letter0;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return result;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
*
|
|
90
|
-
* @param {string} string
|
|
91
|
-
* @returns {string}
|
|
92
|
-
*/
|
|
93
|
-
export function camelToKebab(string) {
|
|
94
|
-
return string.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
*
|
|
100
|
-
* @param {string} string
|
|
101
|
-
* @returns {string}
|
|
102
|
-
*/
|
|
103
|
-
export function kebabToUnderscore(string) {
|
|
104
|
-
return string.replace('-', '_');
|
|
105
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { camelToKebab, capitalize, computeCommonPrefix } from "./StringUtils.js";
|
|
2
|
-
|
|
3
|
-
test("common prefix", () => {
|
|
4
|
-
expect(computeCommonPrefix([])).toBe("");
|
|
5
|
-
|
|
6
|
-
expect(computeCommonPrefix([""])).toBe("");
|
|
7
|
-
|
|
8
|
-
expect(computeCommonPrefix(["a"])).toBe("a");
|
|
9
|
-
|
|
10
|
-
expect(computeCommonPrefix(["abc"])).toBe("abc");
|
|
11
|
-
|
|
12
|
-
expect(computeCommonPrefix(["aa"])).toBe("aa");
|
|
13
|
-
|
|
14
|
-
expect(computeCommonPrefix(["aa", "ab"])).toBe("a");
|
|
15
|
-
|
|
16
|
-
expect(computeCommonPrefix(["abc", "abb"])).toBe("ab");
|
|
17
|
-
|
|
18
|
-
expect(computeCommonPrefix(["abc", "abc"])).toBe("abc");
|
|
19
|
-
|
|
20
|
-
expect(computeCommonPrefix(["abc", "abc", "a"])).toBe("a");
|
|
21
|
-
|
|
22
|
-
expect(computeCommonPrefix(["abc", "abc", ""])).toBe("");
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
test("capitalize", () => {
|
|
26
|
-
expect(capitalize("")).toBe("");
|
|
27
|
-
|
|
28
|
-
expect(capitalize("a")).toBe("A");
|
|
29
|
-
|
|
30
|
-
expect(capitalize("A")).toBe("A");
|
|
31
|
-
|
|
32
|
-
expect(capitalize("aa")).toBe("Aa");
|
|
33
|
-
|
|
34
|
-
expect(capitalize("AA")).toBe("AA");
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
test('camelToKebab', () => {
|
|
38
|
-
expect(camelToKebab('a')).toBe('a');
|
|
39
|
-
expect(camelToKebab('aA')).toBe('a-a');
|
|
40
|
-
expect(camelToKebab('helloWoRLD')).toBe('hello-wo-rld');
|
|
41
|
-
expect(camelToKebab('A')).toBe('a');
|
|
42
|
-
});
|