Snap vector direction to nearest axis

I have this case when I need to get my character direction is snapped to the nearest axis. Kinda look like this.

My solution is to find the largest axis absolute value of axis. Then, change the largest one to 1 and the others 2 with 0. Change largest axis sign with its original sign.

Example, we have Vector3(-0.5f, 0.1f, 0). The snapped direction is Vector3(-1,0,0).

Vector3 SnappedToNearestAxis(Vector3 direction){
    float x = Abs(direction.x);
    float y = Abs(direction.y);
    float z = Abs(direction.z);
    if (x > y && x > z){
        return Vector3(Sign(direction.x),0,0);
    } else if (y > x && y > z){
        return Vector3(0,Sign(direction.y),0);
    } else {
        return Vector3(0,0,Sign(direction.z));
    }
}