Abstracted out common code into `innerTransform`

As per feedback brought common logic from both `transform` and `transformAsObject` into its own `innerTransform` function.